<!DOCTYPE HTML>
<html lang="Chinese">


<head>
    <meta charset="utf-8">
    <meta name="keywords" content="java基础, 博客">
    <meta name="description" content="In me the tiger sniffs the rose.">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
    <meta name="renderer" content="webkit|ie-stand|ie-comp">
    <meta name="mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
    <!-- Global site tag (gtag.js) - Google Analytics -->


    <title>java基础 | 凡诚</title>
    <link rel="icon" type="image/png" href="/favicon.png">

    <link rel="stylesheet" type="text/css" href="/libs/awesome/css/all.css">
    <link rel="stylesheet" type="text/css" href="/libs/materialize/materialize.min.css">
    <link rel="stylesheet" type="text/css" href="/libs/aos/aos.css">
    <link rel="stylesheet" type="text/css" href="/libs/animate/animate.min.css">
    <link rel="stylesheet" type="text/css" href="/libs/lightGallery/css/lightgallery.min.css">
    <link rel="stylesheet" type="text/css" href="/css/matery.css">
    <link rel="stylesheet" type="text/css" href="/css/my.css">
    <script src="https://sdk.jinrishici.com/v2/browser/jinrishici.js" charset="utf-8"></script>
    <script src="/libs/jquery/jquery.min.js"></script>

<meta name="generator" content="Hexo 5.4.2"></head>



   <style>
    body{
       background-image: url(https://cdn.jsdelivr.net/gh/Tokisaki-Galaxy/res/site/medias/background.jpg);
       background-repeat:no-repeat;
       background-size:cover;
       background-attachment:fixed;
    }
</style>



<body>
    <header class="navbar-fixed">
    <nav id="headNav" class="bg-color nav-transparent">
        <div id="navContainer" class="nav-wrapper container">
            <div class="brand-logo">
                <a href="/" class="waves-effect waves-light">
                    
                    <img src="/medias/comment_bg.png" class="logo-img" alt="LOGO">
                    
                    <span class="logo-span">凡诚</span>
                </a>
            </div>
            

<a href="#" data-target="mobile-nav" class="sidenav-trigger button-collapse"><i class="fas fa-bars"></i></a>
<ul class="right nav-menu">
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/" class="waves-effect waves-light">
      
      <i class="fas fa-home" style="zoom: 0.6;"></i>
      
      <span>主页</span>
    </a>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/tags" class="waves-effect waves-light">
      
      <i class="fas fa-tags" style="zoom: 0.6;"></i>
      
      <span>文章</span>
    </a>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/categories" class="waves-effect waves-light">
      
      <i class="fas fa-bookmark" style="zoom: 0.6;"></i>
      
      <span>分类</span>
    </a>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/archives" class="waves-effect waves-light">
      
      <i class="fas fa-archive" style="zoom: 0.6;"></i>
      
      <span>归档</span>
    </a>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="" class="waves-effect waves-light">

      
      <i class="fas fa-user-circle" style="zoom: 0.6;"></i>
      
      <span>关于我</span>
      <i class="fas fa-chevron-down" aria-hidden="true" style="zoom: 0.6;"></i>
    </a>
    <ul class="sub-nav menus_item_child ">
      
      <li>
        <a href="/about">
          
          <i class="fas fa-user-circle" style="margin-top: -20px; zoom: 0.6;"></i>
          
          <span>我的信息</span>
        </a>
      </li>
      
      <li>
        <a href="/contact">
          
          <i class="fas fa-comments" style="margin-top: -20px; zoom: 0.6;"></i>
          
          <span>留言板</span>
        </a>
      </li>
      
      <li>
        <a href="/friends">
          
          <i class="fas fa-address-book" style="margin-top: -20px; zoom: 0.6;"></i>
          
          <span>友链</span>
        </a>
      </li>
      
    </ul>
    
  </li>
  
  <li>
    <a href="#searchModal" class="modal-trigger waves-effect waves-light">
      <i id="searchIcon" class="fas fa-search" title="Search" style="zoom: 0.85;"></i>
    </a>
  </li>
</ul>


<div id="mobile-nav" class="side-nav sidenav">

    <div class="mobile-head bg-color">
        
        <img src="/medias/comment_bg.png" class="logo-img circle responsive-img">
        
        <div class="logo-name">凡诚</div>
        <div class="logo-desc">
            
            In me the tiger sniffs the rose.
            
        </div>
    </div>

    

    <ul class="menu-list mobile-menu-list">
        
        <li class="m-nav-item">
	  
		<a href="/" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-home"></i>
			
			主页
		</a>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="/tags" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-tags"></i>
			
			文章
		</a>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="/categories" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-bookmark"></i>
			
			分类
		</a>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="/archives" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-archive"></i>
			
			归档
		</a>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="javascript:;">
			
				<i class="fa-fw fas fa-user-circle"></i>
			
			关于我
			<span class="m-icon"><i class="fas fa-chevron-right"></i></span>
		</a>
            <ul  style="background:  ;" >
              
                <li>

                  <a href="/about " style="margin-left:75px">
				  
				   <i class="fa fas fa-user-circle" style="position: absolute;left:50px" ></i>
			      
		          <span>我的信息</span>
                  </a>
                </li>
              
                <li>

                  <a href="/contact " style="margin-left:75px">
				  
				   <i class="fa fas fa-comments" style="position: absolute;left:50px" ></i>
			      
		          <span>留言板</span>
                  </a>
                </li>
              
                <li>

                  <a href="/friends " style="margin-left:75px">
				  
				   <i class="fa fas fa-address-book" style="position: absolute;left:50px" ></i>
			      
		          <span>友链</span>
                  </a>
                </li>
              
            </ul>
          
        </li>
        
        
    </ul>
</div>


        </div>

        
    </nav>

</header>

    



<div class="bg-cover pd-header post-cover" style="background-image: url('/medias/featureimages/2.jpg')">
    <div class="container" style="right: 0px;left: 0px;">
        <div class="row">
            <div class="col s12 m12 l12">
                <div class="brand">
                    <h1 class="description center-align post-title">java基础</h1>
                </div>
            </div>
        </div>
    </div>
</div>




<main class="post-container content">

    
    <link rel="stylesheet" href="/libs/tocbot/tocbot.css">
<style>
    #articleContent h1::before,
    #articleContent h2::before,
    #articleContent h3::before,
    #articleContent h4::before,
    #articleContent h5::before,
    #articleContent h6::before {
        display: block;
        content: " ";
        height: 100px;
        margin-top: -100px;
        visibility: hidden;
    }

    #articleContent :focus {
        outline: none;
    }

    .toc-fixed {
        position: fixed;
        top: 64px;
    }

    .toc-widget {
        width: 345px;
        padding-left: 20px;
    }

    .toc-widget .toc-title {
        padding: 35px 0 15px 17px;
        font-size: 1.5rem;
        font-weight: bold;
        line-height: 1.5rem;
    }

    .toc-widget ol {
        padding: 0;
        list-style: none;
    }

    #toc-content {
        padding-bottom: 30px;
        overflow: auto;
    }

    #toc-content ol {
        padding-left: 10px;
    }

    #toc-content ol li {
        padding-left: 10px;
    }

    #toc-content .toc-link:hover {
        color: #42b983;
        font-weight: 700;
        text-decoration: underline;
    }

    #toc-content .toc-link::before {
        background-color: transparent;
        max-height: 25px;

        position: absolute;
        right: 23.5vw;
        display: block;
    }

    #toc-content .is-active-link {
        color: #42b983;
    }

    #floating-toc-btn {
        position: fixed;
        right: 15px;
        bottom: 76px;
        padding-top: 15px;
        margin-bottom: 0;
        z-index: 998;
    }

    #floating-toc-btn .btn-floating {
        width: 48px;
        height: 48px;
    }

    #floating-toc-btn .btn-floating i {
        line-height: 48px;
        font-size: 1.4rem;
    }
</style>
<div class="row">
    <div id="main-content" class="col s12 m12 l9">
        <!-- 文章内容详情 -->
<div id="artDetail">
    <div class="card">
        <div class="card-content article-info">
            <div class="row tag-cate">
                <div class="col s7">
                    
                    <div class="article-tag">
                        
                            <a href="/tags/java/">
                                <span class="chip bg-color">java</span>
                            </a>
                        
                    </div>
                    
                </div>
                <div class="col s5 right-align">
                    
                    <div class="post-cate">
                        <i class="fas fa-bookmark fa-fw icon-category"></i>
                        
                            <a href="/categories/%E5%90%8E%E7%AB%AF%E6%8A%80%E6%9C%AF/" class="post-category">
                                后端技术
                            </a>
                        
                    </div>
                    
                </div>
            </div>

            <div class="post-info">
                
                <div class="post-date info-break-policy">
                    <i class="far fa-calendar-minus fa-fw"></i>Publish Date:&nbsp;&nbsp;
                    2020-05-08
                </div>
                

                
                <div class="post-date info-break-policy">
                    <i class="far fa-calendar-check fa-fw"></i>Update Date:&nbsp;&nbsp;
                    2022-11-10
                </div>
                

                
                <div class="info-break-policy">
                    <i class="far fa-file-word fa-fw"></i>Word Count:&nbsp;&nbsp;
                    31.1k
                </div>
                

                
                <div class="info-break-policy">
                    <i class="far fa-clock fa-fw"></i>Read Times:&nbsp;&nbsp;
                    118 Min
                </div>
                

                
                    <div id="busuanzi_container_page_pv" class="info-break-policy">
                        <i class="far fa-eye fa-fw"></i>Read Count:&nbsp;&nbsp;
                        <span id="busuanzi_value_page_pv"></span>
                    </div>
				
            </div>
        </div>
        <hr class="clearfix">

        
        <!-- 是否加载使用自带的 prismjs. -->
        <link rel="stylesheet" href="/libs/prism/prism.css">
        

        

        <div class="card-content article-card-content">
            <div id="articleContent">
                <p>Java基础</p>
<h2 id="Java概述"><a href="#Java概述" class="headerlink" title="Java概述"></a>Java概述</h2><blockquote>
<p>程序：计算机执行某些操作解决问题而编写的指令的集合。</p>
</blockquote>
<h3 id="Java主要特点"><a href="#Java主要特点" class="headerlink" title="Java主要特点"></a>Java主要特点</h3><ol>
<li><p>Java语言是面向对象的（oop）。</p>
</li>
<li><p>Java语言是健壮的         —-       Java的强类型机制，异常处理机制，垃圾的自动收集是java程序健壮性的重要保证。</p>
</li>
<li><p>Java语言是跨平台性的。</p>
</li>
<li><p>Java语言是解释类型的。</p>
<p>解释性语言：Javascript，PHP，Java       编译性语言： c  / c++</p>
<p>解释性语言和编译性语言的区别：</p>
<pre><code>==解释性语言编译后的代码不能直接由机器执行，需要由解释器来执行，编译性语言，编译后的代码可以直接被机器执行。== 
</code></pre>
</li>
</ol>
<h3 id="Java运行机制及运行过程"><a href="#Java运行机制及运行过程" class="headerlink" title="Java运行机制及运行过程"></a>Java运行机制及运行过程</h3><blockquote>
<p>jave 核心机制-Java虚拟机（JVM）</p>
<p>JVM是一个虚拟的计算机，具有指令集并使用不同的存储区域。负责执行指令，管理数据，内存，寄存器，包含在JDK中。</p>
<p>对于不同的平台，有不同的虚拟机。</p>
<p>Java虚拟机机制同步了底层运行平台的差别，实现了一次编译到处运行。</p>
</blockquote>
<p>运行过程： 将编写的java文件编译为字节码文件，在使用解释器运行。</p>
<h3 id="JDK，JRE，JVM简介"><a href="#JDK，JRE，JVM简介" class="headerlink" title="JDK，JRE，JVM简介"></a>JDK，JRE，JVM简介</h3><ul>
<li><p>JDK基本介绍</p>
<ol>
<li><p>JDK的全称是Java Development Kit(开发者工具包);</p>
<p>JDK = JRE + java的开发工具[Java，javac，javadoc，javap]</p>
</li>
<li><p>JDK是提供给开发人员使用的，其中包括了Java的开发工具，也包括了JRE。</p>
</li>
</ol>
</li>
<li><p>JRE基本介绍</p>
<ol>
<li><p>JRE的全称是Java Runtime Environment （Java运行环境）</p>
<p>JRE = JVM + java核心类库</p>
</li>
<li><p>包括Java虚拟机和Java程序所需运行的核心类库，如果想运行一个开发好的Java程序，计算机中只需要安装JRE即可。</p>
</li>
</ol>
</li>
</ul>
<h2 id="java开发注意事项和细节说明"><a href="#java开发注意事项和细节说明" class="headerlink" title="java开发注意事项和细节说明"></a>java开发注意事项和细节说明</h2><ul>
<li>java源文件以.java为扩展名，源文件的基本组成部分是类(class)。</li>
<li>java应用程序的执行入口是main()方法。他有固定的书写格式。</li>
</ul>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">mian</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> srgs<span class="token punctuation">)</span><span class="token punctuation">&#123;</span>。。。。<span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>



<ul>
<li>java语言严格区分大小写。</li>
<li>Java语言由一条条语句构成，每个语句以“；”结束。</li>
<li>大括号都是成对出现的。</li>
<li>一个源文件只能有一个public类，其他的类的个数不限。</li>
<li>如果源文件中包含一个public类，则文件名必须按照该类名命名。</li>
<li>一个源文件只能有一个public类，其他的类的个数不限，也可以将main方法写在别的public类中，然后指定运行非public类，这样入口方法就是非public的main方法。</li>
</ul>
<h2 id="一-java语法"><a href="#一-java语法" class="headerlink" title="一,java语法"></a>一,java语法</h2><h3 id="1-Java转义字符"><a href="#1-Java转义字符" class="headerlink" title="1. Java转义字符"></a>1. Java转义字符</h3><p>常用转义字符：</p>
<table>
<thead>
<tr>
<th>转义字符</th>
<th>意义</th>
<th>备注</th>
</tr>
</thead>
<tbody><tr>
<td>\t</td>
<td>一个制表符</td>
<td></td>
</tr>
<tr>
<td>\n</td>
<td>换行符</td>
<td></td>
</tr>
<tr>
<td>\\</td>
<td>一个反斜杠</td>
<td></td>
</tr>
<tr>
<td>\“</td>
<td>一个双引号</td>
<td></td>
</tr>
<tr>
<td>\‘</td>
<td>一个单引号</td>
<td></td>
</tr>
<tr>
<td>\r</td>
<td>一个回车</td>
<td>比较特殊</td>
</tr>
</tbody></table>
<p>\n是换行符，这并不是java中的规定，而是更广泛的计算机领域中均为此含义。<br>1、提到\n就不能不提\r（回车），下面，详细讲解一下：<br>在计算机还没有出现之前，有一种叫做电传打字机（Teletype Model 33）的玩意，每秒钟可以打10个字符。但是它有一个问题，就是打完一行换行的时候，要用去0.2秒，正好可以打两个字符。要是在这0.2秒里面，又有新的字符传过来，那么这个字符将丢失。<br>于是，研制人员想了个办法解决这个问题，就是在每行后面加两个表示结束的字符。一个叫做“回车”，告诉打字机把打印头定位在左边界；另一个叫做“换行”，告诉打字机把纸向下移一行。<br>这就是“换行”和“回车”的来历，从它们的英语名字上也可以看出一二。<br>后来，计算机发明了，这两个概念也就被般到了计算机上。那时，存储器很贵，一些科学家认为在每行结尾加两个字符太浪费了，加一个就可以。于是，就出现了分歧。<br>Unix系统里，每行结尾只有“&lt;换行&gt;”，即“\n”；<br>Windows系统里面，每行结尾是“&lt;换行&gt;&lt;回车&gt;”，即“\n\r”；<br>Mac系统里，每行结尾是“&lt;回车&gt;”。一个直接后果是，Unix/Mac系统下的文件在Windows里打开的话，所有文字会变成一行；而Windows里的文件在Unix/Mac下打开的话，在每行的结尾可能会多出一个^M符号。<br>\r回车是将光标移到一行的前面<br>\n是移到下一行</p>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">Changechar</span><span class="token punctuation">&#123;</span>
	<span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> arge<span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
		<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"生活你好\r世界"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>  <span class="token comment">//输出：世界你好</span>
	<span class="token punctuation">&#125;</span>
<span class="token punctuation">&#125;</span>
<span class="token comment">// \r会使输出光标重新返回改行前面，使后输出的内容覆盖已经输出的内容</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>



<h3 id="2-Java注释"><a href="#2-Java注释" class="headerlink" title="2. Java注释"></a>2. Java注释</h3><blockquote>
<p>注释是解释程序的文字，注释提高了代码的可读性，将自己的思想通过注释整理出来，再用代码去体现。</p>
<p>被注释的语句不会被JVM虚拟机执行。</p>
</blockquote>
<h4 id="1-单行注释"><a href="#1-单行注释" class="headerlink" title="1.单行注释"></a>1.单行注释</h4><pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token comment">//这是一个单行注释</span><span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>

<h4 id="2-多行注释"><a href="#2-多行注释" class="headerlink" title="2.多行注释"></a>2.多行注释</h4><pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token comment">/* 这是一个多行注释  */</span><span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>

<ul>
<li>多行注释不能嵌套</li>
</ul>
<h4 id="3-文档注释"><a href="#3-文档注释" class="headerlink" title="3.文档注释"></a>3.文档注释</h4><blockquote>
<p>文档注释内容可以被JDK所提供的工具javadoc所解析，生成一套以网页文件形式体现的该程序的说明文档，一般写在类中。</p>
</blockquote>
<p>基本格式：</p>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token comment">/**
 *@author
 *@version
 */</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span></span></code></pre>

<p>生成对应的文档注释：</p>
<pre class="line-numbers language-cmd" data-language="cmd"><code class="language-cmd">javadoc -d 生成后放置目录 -author -version 文件名.java<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>

<h3 id="3-变量"><a href="#3-变量" class="headerlink" title="3. 变量"></a>3. 变量</h3><blockquote>
<p>变量是程序的基本组成单位。</p>
</blockquote>
<p>变量相当于内存中一个数据存储空间的表示，可以通过变量名访问到变量值。不同的变量，类型不同占用的空间大小也不同。(int占据四个字节，double占据八个字节)</p>
<p>该区域必须先声明，后使用。不允许在同一个作用域中重名。</p>
<p>==变量 = 变量名 + 值 + 数据类型==</p>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">Var</span><span class="token punctuation">&#123;</span>
<span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> arge<span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
	<span class="token keyword">int</span> a<span class="token punctuation">;</span>     <span class="token comment">//第一种方式</span>
 a <span class="token operator">=</span> <span class="token number">520</span><span class="token punctuation">;</span>    <span class="token comment">//第二种方式</span>
 <span class="token keyword">int</span> b <span class="token operator">=</span> <span class="token number">521</span><span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span>
<span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>



<h3 id="4-数据类型"><a href="#4-数据类型" class="headerlink" title="4.数据类型"></a>4.数据类型</h3><blockquote>
<p>每一种数据都定义了明确的数据类型，在内存中分配大小不同的内存空间(字节).</p>
</blockquote>
<p>Java数据类型：</p>
<ol>
<li> 基本数据类型：</li>
</ol>
<ul>
<li>数值型<ul>
<li>整数类型 （byte [1],short [2],int [4],long [8]）</li>
<li>浮点类型 （float [4],double [8]）</li>
</ul>
</li>
<li>字符型 （char [2]存放单个字符）</li>
<li>布尔型 （boolean [1] true，false）<ol start="2">
<li>引用数据类型：</li>
</ol>
</li>
<li>类 class</li>
<li>接口 interface</li>
<li>数组 []</li>
</ul>
<h4 id="整型"><a href="#整型" class="headerlink" title="整型"></a>整型</h4><blockquote>
<p>java各整数类型有固定的范围和字段长度，不受具体的os的影响，以保证java程序的可移植性。</p>
<p>java中的==整形默认常量为int型==，声明long类型常量后需要加 L或者l。</p>
<p>java程序中变量常声明为int型。</p>
</blockquote>
<h4 id="浮点型"><a href="#浮点型" class="headerlink" title="浮点型"></a>浮点型</h4><p>浮点数在机器的存放形式： 浮点数 = 符号位 + 指数位 + 尾数位</p>
<p>java各浮点数类型有固定的范围和字段长度，不受具体的os的影响，以保证java程序的可移植性。</p>
<p>java中的==浮点数默认常量为double型==，声明float类型常量后需要加 F或者f。</p>
<p>尾数部分可能丢失。</p>
<p>==对运算结果是小数的进行相等判断要小心，应该是求两个数的差值的绝对值在某个精度范围内。==</p>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token class-name">Math</span><span class="token punctuation">.</span><span class="token function">abs</span><span class="token punctuation">(</span>num1 <span class="token operator">-</span> num2<span class="token punctuation">)</span><span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>



<h4 id="字符类型"><a href="#字符类型" class="headerlink" title="字符类型"></a>字符类型</h4><blockquote>
<p>字符类型可以表示单个字符，字符类型是char是两个字节，可以存放汉字，多个字符使用String。</p>
</blockquote>
<ul>
<li>字符常量是用单引号括起来的单个字符。</li>
<li>Java允许使用转义字符’\‘来将其后的字符转变为特殊字符型常量。</li>
</ul>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">char</span> c <span class="token operator">=</span> <span class="token char">'\n'</span><span class="token punctuation">;</span>    <span class="token comment">// 表示换行符</span><span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>

<ul>
<li>在java中，char的本质就是一个整数，在输出的时候，是unicode码对应的字符。</li>
<li>可以给char赋一个整数，然后在输出的时候会按照对应的unicode字符进行输出。</li>
<li>char类型可以进行运算，相当于一个整数。</li>
</ul>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">Changechar</span><span class="token punctuation">&#123;</span>
	<span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> arge<span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
	<span class="token keyword">char</span> c <span class="token operator">=</span> <span class="token number">97</span><span class="token punctuation">;</span>
	<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>c<span class="token punctuation">)</span><span class="token punctuation">;</span>    <span class="token comment">//会输出97所编码的字符</span>
	<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token char">'a'</span> <span class="token operator">+</span> <span class="token number">100</span><span class="token punctuation">)</span><span class="token punctuation">;</span>   <span class="token comment">//得出结果197  </span>
	<span class="token punctuation">&#125;</span>
<span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>



<ul>
<li><p>字符型存储到计算机中：</p>
<ul>
<li>‘a’ —&gt; 码值97 —&gt; 转换为二进制 —-&gt;</li>
</ul>
</li>
<li><p>常用编码：</p>
<ul>
<li><p>ASCII: 使用一个字节表示，一共128个字符，实际上可以表示256个字符只使用了128个</p>
</li>
<li><p>Unicode： 固定大小编码，使用两个字节表示，字母和汉字统一都占两个字节。Unicode兼容ASCII</p>
</li>
<li><p>utf-8：大小可变编码，字母使用一个字节，汉字使用三个字节。</p>
</li>
<li><p>gbk： 大小可变，字母使用一个字节，汉字使用两个字节。</p>
</li>
<li><p>gb312：可以表示汉字，gb2312 &lt; gbk。</p>
</li>
<li><p>big5：繁体中文。</p>
</li>
</ul>
</li>
</ul>
<h4 id="boolean类型"><a href="#boolean类型" class="headerlink" title="boolean类型"></a>boolean类型</h4><p>布尔类型只允许使用ture和false，无null</p>
<p>boolean类型占一个字节。</p>
<p>boolean类型适用于逻辑运算，一般用于流程控制。</p>
<p>==不可以用0或者其他数字代替。==</p>
<h3 id="5-加号的使用"><a href="#5-加号的使用" class="headerlink" title="5. 加号的使用"></a>5. 加号的使用</h3><blockquote>
<p>当左右两边都是数值型时，做加法运算。</p>
<p>当左右两边有一方为字符串时，为拼接运算。</p>
</blockquote>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">Changechar</span><span class="token punctuation">&#123;</span>
	<span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> arge<span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
		<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token number">100</span> <span class="token operator">+</span> <span class="token number">98</span><span class="token punctuation">)</span><span class="token punctuation">;</span>   <span class="token comment">//输出加法运算</span>
		<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"100"</span> <span class="token operator">+</span> <span class="token number">98</span><span class="token punctuation">)</span><span class="token punctuation">;</span>	<span class="token comment">//字符串拼接</span>
		<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token number">100</span> <span class="token operator">+</span> <span class="token number">98</span> <span class="token operator">+</span> <span class="token string">"hello"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>    <span class="token comment">//先加法再字符串拼接</span>
		<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"hello"</span> <span class="token operator">+</span> <span class="token number">100</span> <span class="token operator">+</span> <span class="token number">98</span><span class="token punctuation">)</span><span class="token punctuation">;</span> 	<span class="token comment">//先字符串拼接，再字符串拼接</span>
      	<span class="token keyword">char</span> a <span class="token operator">=</span> <span class="token char">'男'</span><span class="token punctuation">;</span>
      	<span class="token keyword">char</span> b <span class="token operator">=</span> <span class="token char">'女'</span><span class="token punctuation">;</span>
      	<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>a <span class="token operator">+</span> b<span class="token punctuation">)</span><span class="token punctuation">;</span> 	<span class="token comment">//cahr类型进行运算先转换为int类型，以码值相加</span>
          
	<span class="token punctuation">&#125;</span>
<span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>







<h3 id="6-基本数据类型转换"><a href="#6-基本数据类型转换" class="headerlink" title="6. 基本数据类型转换"></a>6. 基本数据类型转换</h3><blockquote>
<p>当java程序在进行赋值或者运算时，精度小的类型可以自动转换为精度大的数据类型，这个就是==自动类型转换==。</p>
<p>数据类型按精度大小排序：</p>
<p>char &lt; int &lt; long &lt; float &lt; double</p>
<p>byte &lt; short &lt; int &lt; long &lt; float &lt; double</p>
</blockquote>
<h4 id="自动数据类型转换"><a href="#自动数据类型转换" class="headerlink" title="自动数据类型转换"></a>自动数据类型转换</h4><blockquote>
<p>当有多种类型的数据混合运算的时候，系统首先将所有数据转换成容量最大的那种数据类型，然后再进行计算。</p>
<p>当把精度大的数据类型赋值给精度小的数据类型时会报错，将精度低的赋值给精度高的是，会自动类型转换。</p>
</blockquote>
<p>boolean不参与转换。</p>
<p>==byte，short和char他们三者可以计算，在计算时首先转换为int，不论是单独还是混合出现。==</p>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token comment">//给byte类型变量赋值时，先判断该数是否再byte范围中，如果可以就成功</span>
<span class="token keyword">byte</span> b <span class="token operator">=</span> <span class="token number">10</span><span class="token punctuation">;</span>               <span class="token comment">//short s = 10；</span>
<span class="token comment">//byte类型精度不足以存放int类型数值</span>
<span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">10</span><span class="token punctuation">;</span>
<span class="token keyword">byte</span> b <span class="token operator">=</span> i<span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>byte 和 short 类型数据进行运算后，精度会被直接提升到int</p>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">byte</span> b <span class="token operator">=</span> <span class="token number">10</span><span class="token punctuation">;</span>
<span class="token keyword">short</span> s <span class="token operator">=</span> <span class="token number">10</span>；
<span class="token keyword">int</span> i <span class="token operator">=</span> b <span class="token operator">+</span> s<span class="token punctuation">;</span> <span class="token comment">//byte 和 short 类型数据进行运算后，精度会被直接提升到int</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span></span></code></pre>

<h4 id="强制类型转换"><a href="#强制类型转换" class="headerlink" title="强制类型转换"></a>强制类型转换</h4><blockquote>
<p>自动类型转换的逆过程，将容量大的数据库类型转换为容量小的数据类型。使用时需要加上强制类型转换符</p>
<p>() 可能造成精度降低或者溢出，要格外小心。</p>
</blockquote>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token keyword">int</span><span class="token punctuation">)</span><span class="token number">1.9</span><span class="token punctuation">;</span>   <span class="token comment">//会将小数位删除造成数据丢失</span><span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>

<p>强制符号只针对最近的操作数有效，往往使用小括号提升优先级</p>
<p>==char类型可以保存int类型的常量值，但是不能保存int类型的变量值，需要使用强制类型转换。==</p>
<p>byte和short，char类型进行运算的时候，当成int类型处理。</p>
<h3 id="7-基本数据类型转换为String"><a href="#7-基本数据类型转换为String" class="headerlink" title="7. 基本数据类型转换为String"></a>7. 基本数据类型转换为String</h3><blockquote>
<p>经常需要将基本数据类型转换为字符串，或者将字符串转换为基本数据类型。</p>
<p>基本数据类型转换为字符串只需要将值加上 “ “ .</p>
</blockquote>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token class-name">String</span> s1 <span class="token operator">=</span> <span class="token number">1</span> <span class="token operator">+</span> <span class="token string">""</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>



<p>String类型转换为基本数据类型需要调用包装类的parseXX方法。</p>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token class-name">Integer</span><span class="token punctuation">.</span><span class="token function">parseInt</span><span class="token punctuation">(</span><span class="token string">""</span><span class="token punctuation">)</span><span class="token punctuation">;</span>    <span class="token comment">//如果不能成功转换抛出异常</span>
<span class="token class-name">Double</span><span class="token punctuation">.</span><span class="token function">parseDouble</span><span class="token punctuation">(</span><span class="token string">""</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token class-name">Float</span><span class="token punctuation">.</span><span class="token function">parseFloat</span><span class="token punctuation">(</span><span class="token string">""</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token class-name">Short</span><span class="token punctuation">.</span><span class="token function">parseShort</span><span class="token punctuation">(</span><span class="token string">""</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token class-name">Long<span class="token punctuation">.</span>ParseLong</span><span class="token punctuation">(</span><span class="token string">""</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token class-name">Boolean</span><span class="token punctuation">.</span><span class="token function">parseBoolean</span><span class="token punctuation">(</span><span class="token string">""</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token class-name">Byte<span class="token punctuation">.</span>ParseByte</span><span class="token punctuation">(</span><span class="token string">""</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">//char类型通过取出字符串类型中的字符得到</span>
变量<span class="token punctuation">.</span><span class="token function">charAt</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>







<h3 id="8-运算符"><a href="#8-运算符" class="headerlink" title="8. 运算符"></a>8. 运算符</h3><blockquote>
<p>运算符是一种特殊的符号，用以表似乎数据的运算，赋值和比较等。</p>
<p>运算符有：</p>
<p>​    算数运算符，赋值运算符，关系运算符(比较运算符)，逻辑运算符，位运算符，三元运算符</p>
</blockquote>
<h4 id="8-1算数运算符"><a href="#8-1算数运算符" class="headerlink" title="8.1算数运算符"></a>8.1算数运算符</h4><table>
<thead>
<tr>
<th align="center">描述</th>
<th>运算符</th>
</tr>
</thead>
<tbody><tr>
<td align="center">加-两个对象相加</td>
<td>+</td>
</tr>
<tr>
<td align="center">减-得到复数或是一个数减去另一个数</td>
<td>-</td>
</tr>
<tr>
<td align="center">乘-两个数相乘或是返回一个被重复若干次的字符串</td>
<td>*</td>
</tr>
<tr>
<td align="center">除-X除以Y，两个整数相除得到一个浮点数</td>
<td>/</td>
</tr>
<tr>
<td align="center">取模-返回除法的余数 本质： a % b = a - a /b * b</td>
<td>%</td>
</tr>
<tr>
<td align="center">幂-返回x的y次幂</td>
<td>**</td>
</tr>
<tr>
<td align="center">取整数- 向下取接近除数的整数</td>
<td>//</td>
</tr>
<tr>
<td align="center">字符串相加</td>
<td>+</td>
</tr>
<tr>
<td align="center">自加运算（分为前加加和后加加）</td>
<td>++</td>
</tr>
<tr>
<td align="center">自减运算（分为前减减和后减减）</td>
<td>–</td>
</tr>
</tbody></table>
<p>如果a%b时a为小数：  a % b = a - （int）a / b * b</p>
<h4 id="8-2关系运算符"><a href="#8-2关系运算符" class="headerlink" title="8.2关系运算符"></a>8.2关系运算符</h4><blockquote>
<p>关系运算符的结果都是boolean类型的，常常使用在if结构中或者循环结构中。</p>
</blockquote>
<table>
<thead>
<tr>
<th align="center">描述</th>
<th>运算符</th>
</tr>
</thead>
<tbody><tr>
<td align="center">等于-比较对象是否相等</td>
<td>==</td>
</tr>
<tr>
<td align="center">不等于-比较两个对象是否不相等</td>
<td>!=</td>
</tr>
<tr>
<td align="center">大于-返回X是否大于Y</td>
<td>&gt;</td>
</tr>
<tr>
<td align="center">小于-返回X是否小于Y(所有比较运算符返回1为真，返回0表示假)</td>
<td>&lt;</td>
</tr>
<tr>
<td align="center">大于等于-返回X是否大于等于Y</td>
<td>&gt;=</td>
</tr>
<tr>
<td align="center">小于等于-返回X是否小于等于Y</td>
<td>&lt;=</td>
</tr>
<tr>
<td align="center">检查是否是类的对象(运行类型)</td>
<td>instanceof</td>
</tr>
</tbody></table>
<h4 id="8-3-逻辑运算符"><a href="#8-3-逻辑运算符" class="headerlink" title="8.3 逻辑运算符"></a>8.3 逻辑运算符</h4><blockquote>
<p>用于链接多个条件，最终结果也是个boolean类型的值。</p>
<p>==短路效率比较高，只运行短路部分。==</p>
</blockquote>
<table>
<thead>
<tr>
<th align="center">运算符</th>
<th>描述</th>
</tr>
</thead>
<tbody><tr>
<td align="center">&amp;</td>
<td>逻辑与</td>
</tr>
<tr>
<td align="center">|</td>
<td>逻辑或</td>
</tr>
<tr>
<td align="center">^</td>
<td>逻辑异或</td>
</tr>
<tr>
<td align="center">&amp;&amp;</td>
<td>短路逻辑与</td>
</tr>
<tr>
<td align="center">||</td>
<td>短路逻辑或</td>
</tr>
<tr>
<td align="center">！</td>
<td>取反</td>
</tr>
</tbody></table>
<h4 id="8-4-赋值运算符"><a href="#8-4-赋值运算符" class="headerlink" title="8.4 赋值运算符"></a>8.4 赋值运算符</h4><p>赋值运算符就是将某个运算后的值，赋给指定的变量。</p>
<p>基本赋值运算符： =</p>
<p>复合赋值运算符： +=，-=，*=，%=</p>
<p>复合赋值运算符会进行类型的转换</p>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">byte</span> b <span class="token operator">=</span> <span class="token number">3</span><span class="token punctuation">;</span>
b <span class="token operator">+=</span> <span class="token number">2</span> <span class="token comment">//  b = (byte)(b + 2)</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre>



<table>
<thead>
<tr>
<th align="center">运算符</th>
<th>描述</th>
</tr>
</thead>
<tbody><tr>
<td align="center">=</td>
<td>等于- a=1</td>
</tr>
<tr>
<td align="center">+=</td>
<td>加法赋值语句-“a+=b” = “a=a+b”</td>
</tr>
<tr>
<td align="center">-=</td>
<td>减法赋值语句-“a-=b” = “a=a-b”</td>
</tr>
<tr>
<td align="center">*=</td>
<td>乘法赋值语句-“a*=b”  = “a=a*b”</td>
</tr>
<tr>
<td align="center">/=</td>
<td>除法赋值语句-“a/=b” = “a=a/b”</td>
</tr>
<tr>
<td align="center">%=</td>
<td>取模赋值语句-“a%=b” = “a=a%b”</td>
</tr>
<tr>
<td align="center">**=</td>
<td>幂赋值语句-“a* *=b” = “a=a * *b”</td>
</tr>
<tr>
<td align="center">//=</td>
<td>加法赋值语句-“a//=b” = “a=a//b”</td>
</tr>
</tbody></table>
<h4 id="8-5-三元运算符"><a href="#8-5-三元运算符" class="headerlink" title="8.5 三元运算符"></a>8.5 三元运算符</h4><blockquote>
<p>条件表达式 ？表达式1 ：表达式2;</p>
</blockquote>
<p>如果条件表达式的结果为true，运算后的结果为表达式1；</p>
<p>如果条件表达式的结果为false，运算后的结果为表达式2；</p>
<p>表达式1和表达式2要为可以赋给接收变量的类型(或可以自动转换)</p>
<p>三元运算符存在变量提升问题</p>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">int</span> a<span class="token punctuation">,</span>b<span class="token punctuation">;</span>
<span class="token keyword">int</span> c <span class="token operator">=</span> a <span class="token operator">></span> b ？a <span class="token operator">:</span> b<span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre>

<p>可以在三元运算符中使用强转：</p>
<pre class="line-numbers language-java" data-language="java"><code class="language-java">a<span class="token punctuation">,</span>b<span class="token punctuation">;</span>
<span class="token keyword">int</span> c <span class="token operator">=</span> a <span class="token operator">></span> b ？<span class="token punctuation">(</span><span class="token keyword">int</span><span class="token punctuation">)</span> <span class="token number">1.1</span><span class="token operator">:</span> <span class="token punctuation">(</span><span class="token keyword">int</span><span class="token punctuation">)</span><span class="token number">2.2</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre>



<h4 id="8-6-运算符的优先级"><a href="#8-6-运算符的优先级" class="headerlink" title="8.6 运算符的优先级"></a>8.6 运算符的优先级</h4><blockquote>
<p>运算符具有不同的优先级。</p>
<p>只有单目运算符和赋值运算符是从右向左执行的。</p>
</blockquote>
<table>
<thead>
<tr>
<th align="center">运算符</th>
<th>描述</th>
</tr>
</thead>
<tbody><tr>
<td align="center">++  –  ~ !(data type)</td>
<td>R —&gt; L</td>
</tr>
<tr>
<td align="center">*  /  %</td>
<td>L —&gt;  R</td>
</tr>
<tr>
<td align="center">+  -</td>
<td>L —&gt;  R</td>
</tr>
<tr>
<td align="center">&gt;&gt; &lt;&lt;  &gt;&gt;&gt;</td>
<td>L —&gt;  R</td>
</tr>
<tr>
<td align="center">&lt;=    &lt;     &gt;   &gt;=  instanceof</td>
<td>L —&gt;  R</td>
</tr>
<tr>
<td align="center">==   !=</td>
<td>L —&gt;  R</td>
</tr>
<tr>
<td align="center">&amp;</td>
<td>L —&gt;  R</td>
</tr>
<tr>
<td align="center">^</td>
<td>L —&gt;  R</td>
</tr>
<tr>
<td align="center">|</td>
<td>L —&gt;  R</td>
</tr>
<tr>
<td align="center">&amp;&amp;</td>
<td>L —&gt;  R</td>
</tr>
<tr>
<td align="center">||</td>
<td>L —&gt;  R</td>
</tr>
<tr>
<td align="center">=    +=   -=   *= /=   %=   &amp;=   ^=  &lt;&lt;=  &gt;&gt;=  &gt;&gt;&gt;=</td>
<td></td>
</tr>
<tr>
<td align="center"></td>
<td></td>
</tr>
<tr>
<td align="center"></td>
<td></td>
</tr>
<tr>
<td align="center"></td>
<td></td>
</tr>
<tr>
<td align="center"></td>
<td></td>
</tr>
</tbody></table>
<h4 id="8-7-位运算"><a href="#8-7-位运算" class="headerlink" title="8.7 位运算"></a>8.7 位运算</h4><table>
<thead>
<tr>
<th align="center">运算符</th>
<th>描述</th>
</tr>
</thead>
<tbody><tr>
<td align="center">&amp;</td>
<td>按位与运算符-    参与运算的两个值相应位为1则结果为1，否则为0</td>
</tr>
<tr>
<td align="center">|</td>
<td>按位或运算符-    参与运算的其中一个值为1，结果为1.</td>
</tr>
<tr>
<td align="center">^</td>
<td>按位异或运算符-  当两相应的二进制位相异时，结果为1.</td>
</tr>
<tr>
<td align="center">~</td>
<td>按位取反运算符-  对数据的每个二进制位取反，即把1变为0，把0变为1.</td>
</tr>
<tr>
<td align="center">&lt;&lt;</td>
<td>左移动运算符-  运算数的各二进制位全部左移若干位由”&lt;&lt;”指定移动的位数。</td>
</tr>
<tr>
<td align="center">&gt;&gt;</td>
<td>右移动运算符-运算数的各二进制位全部右移若干位由”&lt;&lt;”指定移动的位数。</td>
</tr>
<tr>
<td align="center">&gt;&gt;&gt;</td>
<td>无符号右移</td>
</tr>
</tbody></table>
<h4 id="8-8-原码，反码，补码"><a href="#8-8-原码，反码，补码" class="headerlink" title="8.8 原码，反码，补码"></a>8.8 原码，反码，补码</h4><p>二进制的最高位是符号位，0表示正数，1表示负数</p>
<p>正数的源码，补码，反码都一样。(三码合一)</p>
<p>负数的反码 = 它的原码除符号位不变，其余位取反。</p>
<p>复数的补码 = 它的反码 + 1；  复数的反码 = 复数的补码 - 1；</p>
<p>0 的反码，补码都是0.</p>
<p>Java没有无符号数，java中的数都是有符号的。</p>
<p>在计算机运算的时候，都是以补码的方式来运算的。</p>
<p>看运算结果时，要看它的原码。</p>
<h3 id="9-键盘输入语句"><a href="#9-键盘输入语句" class="headerlink" title="9. 键盘输入语句"></a>9. 键盘输入语句</h3><blockquote>
<p>在编程中，需要接收用户输入的数据，就可以使用键盘输入语句来获取/</p>
<p>需要一个扫描器，Scanner。</p>
</blockquote>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token comment">//导入该类所在的包</span>
<span class="token comment">//创建该类的对象</span>
<span class="token comment">//使用方法</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">java<span class="token punctuation">.</span>util<span class="token punctuation">.</span></span><span class="token class-name">Scanner</span></span><span class="token punctuation">;</span>				<span class="token comment">//导入该类所在的包</span>

<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">Changechar</span><span class="token punctuation">&#123;</span>
	<span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> arge<span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
		<span class="token class-name">String</span> name<span class="token punctuation">;</span>
		<span class="token class-name">Scanner</span> sa <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Scanner</span><span class="token punctuation">(</span><span class="token class-name">System</span><span class="token punctuation">.</span>in<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//创建该类的对象，传入标准输入流</span>

		<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">print</span><span class="token punctuation">(</span><span class="token string">"请输入："</span><span class="token punctuation">)</span><span class="token punctuation">;</span> 

		name <span class="token operator">=</span> sa<span class="token punctuation">.</span><span class="token function">next</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>				<span class="token comment">//使用方法</span>

		<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">print</span><span class="token punctuation">(</span>a<span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token punctuation">&#125;</span>
<span class="token punctuation">&#125;</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>





<p>Scanner对象.next()  :                  接收一个字符串</p>
<p>Scanner对象.nextInt()  :                接受一个整数</p>
<p>Scanner对象.nextLong()  :            接收一个长整型  </p>
<p>Scanner对象.nextShort()  :            接收一个短整型</p>
<p>Scanner对象.nextFloat()  :            接收一个单精度</p>
<p>Scanner对象.nextDouble()  :            接收一个双精度</p>
<h3 id="10-控制结构"><a href="#10-控制结构" class="headerlink" title="10. 控制结构"></a>10. 控制结构</h3><blockquote>
<p>在程序中，程序运行的流程控制决定程序是如何执行的，是我们必须掌握的，主要有三大流程控制语句。</p>
</blockquote>
<ol>
<li>顺序控制</li>
<li>分支控制</li>
<li>循环控制</li>
</ol>
<h4 id="10-1-顺序控制"><a href="#10-1-顺序控制" class="headerlink" title="10.1 顺序控制"></a>10.1 顺序控制</h4><blockquote>
<p>程序从上到下逐行的执行，中间没有任何判断和跳转。</p>
<p>向前引用</p>
</blockquote>
<h4 id="10-2-分支控制"><a href="#10-2-分支控制" class="headerlink" title="10.2 分支控制"></a>10.2 分支控制</h4><blockquote>
<p>经过判断指定的条件表达式，达到执行响应代码的作用，分为但分支，双分支和多分支。</p>
</blockquote>
<p>分支控制之间可以嵌套执行。</p>
<p>在一个分支结构中又完整的嵌套了另一个完整的分支结构，里面的分支结构成为内层分支外面的分支结构称为外层分支。</p>
<p>if判断：</p>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">if</span><span class="token punctuation">(</span>条件表达式<span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
执行代码块；
<span class="token punctuation">&#125;</span>
<span class="token keyword">else</span> <span class="token keyword">if</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
执行代码
<span class="token punctuation">&#125;</span>
<span class="token keyword">else</span> <span class="token punctuation">&#123;</span>
执行代码
<span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>switch分支结构</p>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">switch</span><span class="token punctuation">(</span>表达式<span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
<span class="token keyword">case</span> 常量一<span class="token operator">:</span> 语句一<span class="token punctuation">;</span><span class="token keyword">break</span><span class="token punctuation">;</span>
<span class="token keyword">case</span> 常量二<span class="token operator">:</span> 语句二<span class="token punctuation">;</span><span class="token keyword">break</span><span class="token punctuation">;</span> 
<span class="token keyword">case</span> 常量三<span class="token operator">:</span> 语句三<span class="token punctuation">;</span><span class="token keyword">break</span><span class="token punctuation">;</span>
<span class="token keyword">case</span> 常量四<span class="token operator">:</span> 语句四<span class="token punctuation">;</span><span class="token keyword">break</span><span class="token punctuation">;</span>
<span class="token keyword">default</span><span class="token operator">:</span>语句<span class="token punctuation">;</span><span class="token keyword">break</span><span class="token punctuation">;</span>    <span class="token comment">//default后的break不重要，有没有都会退出</span>
<span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>细节：</p>
<ol>
<li>表达式数据类型，应该和case后的常量类型一致，或者可以自动转换成可以比较的类型。</li>
<li>switch(表达式) 中的表达式的返回值必须是==byte，short，int，char，enum，String==。</li>
<li>case后的值必须是常量不能是变量。</li>
<li>break语句用来跳出switch循环，如果没有写会顺序执行到结尾。</li>
</ol>
<p>分支控制的选择：</p>
<ul>
<li>如果判断的具体数值不多，而且符合byte，short，int，char，enum，String。</li>
<li>对于区间判断，对结构未boolean类型判断，使用if，if的范围比较广。</li>
</ul>
<h4 id="10-3-循环控制"><a href="#10-3-循环控制" class="headerlink" title="10.3 循环控制"></a>10.3 循环控制</h4><p>for循环控制：</p>
<ol>
<li>for关键字，表示循环控制。</li>
<li>for有四要素： 循环变量初始化，循环条件，循环操作，循环变量迭代。</li>
</ol>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">for</span><span class="token punctuation">(</span>循环变量初始化<span class="token punctuation">;</span>循环条件<span class="token punctuation">;</span>循环变量迭代<span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
  循环操作<span class="token punctuation">(</span>可以多条语句<span class="token punctuation">)</span>；
<span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span></span></code></pre>

<p>注意：</p>
<ol>
<li>循环条件是返回一个布尔值的表达式。</li>
<li>如果循环变量初始化定义变量，此变量的适用范围只有for循环中。</li>
<li>for(;循环判断条件;) ，中的初始化和变量迭代可以写到其他地方，但是两边的分号不能省略。</li>
<li>循环初始值可以有多条初始化语句，但是要求类型一样，并且中间用逗号隔开，循环变量迭代也可以有多条变量迭代语句，中间用逗号隔开。</li>
</ol>
<p>while循环控制</p>
<pre class="line-numbers language-java" data-language="java"><code class="language-java">循环变量初始化<span class="token punctuation">;</span>
<span class="token keyword">while</span><span class="token punctuation">(</span>循环条件<span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
	循环体<span class="token punctuation">;</span>
循环变量迭代<span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>注意：</p>
<ul>
<li>while也有四要素，只是放置四要素的位置和for循环不一样。</li>
<li>循环条件返回一个布尔值的表达式。</li>
<li>while循环是先判断再循环。</li>
</ul>
<p>do-while循环控制</p>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">do</span><span class="token punctuation">&#123;</span>
循环体<span class="token punctuation">;</span>
循环变量迭代<span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span><span class="token keyword">while</span><span class="token punctuation">(</span>循环条件<span class="token punctuation">)</span>；<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span></span></code></pre>

<p>注意：</p>
<ul>
<li>do-while循环至少会循环一次。</li>
<li>while也有四要素。</li>
</ul>
<h3 id="11-跳转控制语句"><a href="#11-跳转控制语句" class="headerlink" title="11. 跳转控制语句"></a>11. 跳转控制语句</h3><blockquote>
<p> break语句用于==终止某个语句块==的执行，一般用于switch和循环中。</p>
</blockquote>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token punctuation">&#123;</span>
…………
<span class="token keyword">break</span><span class="token punctuation">;</span>
…………
<span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>break语句出现在多层嵌套的语句块中时，可以通过==标签==指明要退出的是哪一层语句块。</p>
<pre class="line-numbers language-java" data-language="java"><code class="language-java">label1：<span class="token punctuation">&#123;</span>
label2： <span class="token punctuation">&#123;</span>
label3： <span class="token punctuation">&#123;</span>
<span class="token keyword">break</span> label2；
<span class="token punctuation">&#125;</span>
<span class="token punctuation">&#125;</span>
<span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>注意：</p>
<ol>
<li>break 语句可以指定推出哪层。</li>
<li>label1 是标签名，名字由人指定。</li>
<li>break 后指定到哪个lable就退出到哪里。</li>
<li>如果没有指定break，则默认推出最近的循环体。</li>
</ol>
<blockquote>
<p> continue语句用于结束本次循环，继续执行下一次循环。</p>
</blockquote>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token punctuation">&#123;</span>
…………
<span class="token keyword">continue</span>；
…………
<span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span></span></code></pre>



<p>注意：</p>
<ol>
<li>Continue语句出现在多层嵌套语句体中时，可以通过标签名指定要跳过的是哪一层循环，和break的标签使用方法相同。</li>
</ol>
<pre class="line-numbers language-java" data-language="java"><code class="language-java">label1：<span class="token punctuation">&#123;</span>
	label2： <span class="token punctuation">&#123;</span>
		label3： <span class="token punctuation">&#123;</span>
			<span class="token keyword">continue</span> label2；
		<span class="token punctuation">&#125;</span>
	<span class="token punctuation">&#125;</span>
<span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>



<blockquote>
<p>return </p>
</blockquote>
<p>return语句表示跳出所在的方法。如果在主方法中写return表示推出程序。</p>
<h3 id="12-数组"><a href="#12-数组" class="headerlink" title="12. 数组"></a>12. 数组</h3><blockquote>
<p>数组可以放置多个同一类型的数据，数组也是一种数据类型，使引用类型。</p>
</blockquote>
<pre class="line-numbers language-java" data-language="java"><code class="language-java">数组类型<span class="token punctuation">[</span><span class="token punctuation">]</span> 数组名 <span class="token operator">=</span> <span class="token punctuation">&#123;</span><span class="token punctuation">&#125;</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>



<blockquote>
<p>可以通过数组名[下标] 来访问数组元素。</p>
<p>下标是从零开始。</p>
<p>数组名.length  属性表示数组的长度</p>
</blockquote>
<p>注意：</p>
<ol>
<li><p>数组是多个==相同类型数据==的组合，实现对这些数据的统一管理。</p>
</li>
<li><p>数组中的元素可以是任何数据类型，包括基本数据类型和引用数据类型，但是不能混用。</p>
</li>
<li><p>数组创建之后，如果没有赋值，有默认值：</p>
<ul>
<li>int 0   short 0 long 0 byte 0 float 0.0 double 0.0 char \u0000 boolean false String null</li>
</ul>
</li>
<li><p>使用数组的步骤： 1.声明数组2.开辟空间 3.给数组各个元素赋值4.使用数组</p>
</li>
<li><p>数组的下标从0开始。</p>
</li>
<li><p>数组下标必须在指定范围内使用，否则抛出异常：下标越界异常，比如：</p>
</li>
</ol>
<p>​    int a[] = new int[5]; 有效下标为0-4.</p>
<ol start="7">
<li><p>数组属于引用类型，数组型数据是对象。</p>
</li>
<li><p>可以通过声明父类型数组，放置类型不同的元素。</p>
</li>
</ol>
<h4 id="12-1-动态初始化"><a href="#12-1-动态初始化" class="headerlink" title="12.1 动态初始化"></a>12.1 动态初始化</h4><p>数据类型[] 数组名 = new 数据类型[大小]；</p>
<p>定义数组可以不定义大小，开辟空间必须定义大小。</p>
<p>==数组初始化未赋值时初始化为0。==</p>
<pre class="line-numbers language-java" data-language="java"><code class="language-java">第一种：<span class="token keyword">int</span><span class="token punctuation">[</span><span class="token punctuation">]</span> a <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token keyword">int</span><span class="token punctuation">[</span><span class="token number">5</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
第二种：<span class="token keyword">int</span> a<span class="token punctuation">[</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token keyword">int</span><span class="token punctuation">[</span><span class="token number">5</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
第三种：<span class="token keyword">int</span> a<span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
	  a <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token keyword">int</span><span class="token punctuation">[</span><span class="token number">5</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token keyword">int</span> i<span class="token punctuation">;</span>
<span class="token class-name">Scanner</span> scan <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Scanner</span><span class="token punctuation">(</span><span class="token class-name">System</span><span class="token punctuation">.</span>in<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">for</span><span class="token punctuation">(</span>i<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">;</span>i<span class="token operator">&lt;</span>a<span class="token punctuation">.</span>length<span class="token punctuation">;</span>i<span class="token operator">++</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
a<span class="token punctuation">[</span>i<span class="token punctuation">]</span> <span class="token operator">=</span> scan<span class="token punctuation">.</span><span class="token function">nextInt</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>



<h4 id="12-2-静态初始化"><a href="#12-2-静态初始化" class="headerlink" title="12.2 静态初始化"></a>12.2 静态初始化</h4><blockquote>
<p>数据类型 数组名[] = {元素值,元素值}</p>
</blockquote>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">int</span> a<span class="token punctuation">[</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token punctuation">&#123;</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">,</span><span class="token number">3</span><span class="token punctuation">&#125;</span><span class="token punctuation">;</span> <span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>





<h4 id="12-3-数组赋值的分配机制"><a href="#12-3-数组赋值的分配机制" class="headerlink" title="12.3 数组赋值的分配机制"></a>12.3 数组赋值的分配机制</h4><blockquote>
<p>基本数据类型赋值，这个值就是具体的数据，不会相互影响。</p>
<p>数组默认情况下时引用传递，赋的是地址，会相互影响。</p>
</blockquote>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">int</span> x <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span>
<span class="token keyword">int</span> y <span class="token operator">=</span> x<span class="token punctuation">;</span>
y <span class="token operator">=</span> <span class="token number">2</span><span class="token punctuation">;</span>				<span class="token comment">//值传递，变量不会相互影响</span>

<span class="token keyword">int</span> a<span class="token punctuation">[</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token punctuation">&#123;</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">,</span><span class="token number">3</span><span class="token punctuation">&#125;</span><span class="token punctuation">;</span>
<span class="token keyword">int</span> b<span class="token punctuation">[</span><span class="token punctuation">]</span> <span class="token operator">=</span> a<span class="token punctuation">;</span> 
b<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token number">2</span><span class="token punctuation">;</span>  <span class="token comment">//址传递，会相互影响</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>





<h3 id="13-二维数组"><a href="#13-二维数组" class="headerlink" title="13 二维数组"></a>13 二维数组</h3><blockquote>
<p>二维数组由多个一维数组组成，他的各个一维数组的长度可以相同，也可以不相同。</p>
<p>二维数组定义时可以不定义大小，开辟空间时必须指定大小。</p>
</blockquote>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">int</span><span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token punctuation">]</span> arr <span class="token operator">=</span> <span class="token punctuation">&#123;</span><span class="token punctuation">&#123;</span><span class="token punctuation">&#125;</span><span class="token punctuation">,</span><span class="token punctuation">&#123;</span><span class="token punctuation">&#125;</span><span class="token punctuation">,</span><span class="token punctuation">&#123;</span><span class="token punctuation">&#125;</span><span class="token punctuation">&#125;</span><span class="token punctuation">;</span>
<span class="token keyword">int</span> arr<span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token punctuation">&#123;</span><span class="token punctuation">&#123;</span><span class="token punctuation">&#125;</span><span class="token punctuation">,</span><span class="token punctuation">&#123;</span><span class="token punctuation">&#125;</span><span class="token punctuation">,</span><span class="token punctuation">&#123;</span><span class="token punctuation">&#125;</span><span class="token punctuation">&#125;</span><span class="token punctuation">;</span>
<span class="token keyword">int</span><span class="token punctuation">[</span><span class="token punctuation">]</span> arr<span class="token punctuation">[</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token punctuation">&#123;</span><span class="token punctuation">&#123;</span><span class="token punctuation">&#125;</span><span class="token punctuation">,</span><span class="token punctuation">&#123;</span><span class="token punctuation">&#125;</span><span class="token punctuation">,</span><span class="token punctuation">&#123;</span><span class="token punctuation">&#125;</span><span class="token punctuation">&#125;</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span></span></code></pre>



<p> 二维数组动态初始化：</p>
<pre class="line-numbers language-java" data-language="java"><code class="language-java">已知二维数组项数
<span class="token keyword">int</span><span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token punctuation">]</span> arr <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token keyword">int</span><span class="token punctuation">[</span><span class="token number">5</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">5</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> i<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">;</span>i<span class="token operator">&lt;</span> arr<span class="token punctuation">.</span>length<span class="token punctuation">;</span>i<span class="token operator">++</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
  <span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> j <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> j <span class="token operator">&lt;</span> arr<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">.</span>length<span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
    arr<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">[</span>j<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token number">1</span>；
  <span class="token punctuation">&#125;</span>
<span class="token punctuation">&#125;</span>
<span class="token keyword">int</span><span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token punctuation">]</span> arr <span class="token operator">=</span> <span class="token punctuation">&#123;</span><span class="token punctuation">&#123;</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">,</span><span class="token number">3</span><span class="token punctuation">&#125;</span><span class="token punctuation">,</span><span class="token punctuation">&#123;</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">&#125;</span><span class="token punctuation">,</span><span class="token punctuation">&#123;</span><span class="token number">1</span><span class="token punctuation">&#125;</span><span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<pre class="line-numbers language-java" data-language="java"><code class="language-java">未知二维数组项数
<span class="token keyword">int</span><span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token punctuation">]</span> arr <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token keyword">int</span><span class="token punctuation">[</span><span class="token number">5</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">;</span>  
<span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> i<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">;</span>i<span class="token operator">&lt;</span> arr<span class="token punctuation">.</span>length<span class="token punctuation">;</span>i<span class="token operator">++</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
 	arr<span class="token punctuation">[</span>i<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token keyword">int</span><span class="token punctuation">[</span>i <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span></span></code></pre>



<h3 id="14-类与对象"><a href="#14-类与对象" class="headerlink" title="14  类与对象"></a>14  类与对象</h3><p>使用类的过程：</p>
<ol>
<li>定义相应的类</li>
<li>创建实例对象<ol>
<li>先加载类信息(属性和方法信息，只会加载一次（在方法区加载），下次创建实例不加载)</li>
<li>在堆中分配空间，进行默认初始化</li>
<li>把堆中地址分配给对象</li>
<li>进行指定初始化</li>
</ol>
</li>
<li>调用类的成员或者方法</li>
</ol>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">class</span> 类名<span class="token punctuation">&#123;</span>
属性；
属性；
方法<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span><span class="token punctuation">&#125;</span><span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span>
<span class="token keyword">public</span> <span class="token keyword">class</span> doubao<span class="token punctuation">&#123;</span>
<span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token keyword">void</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> arge<span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
	类名 对象名 <span class="token operator">=</span> <span class="token keyword">new</span> 类名<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>  \\ 类名 对象名；
	对象名<span class="token punctuation">.</span>属性；				\\ 对象名 <span class="token operator">=</span> <span class="token keyword">new</span> 类名；
	对象名<span class="token punctuation">.</span>方法<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>			  
<span class="token punctuation">&#125;</span>
<span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>java内存分配机制</p>
<p>栈：一般存放基本数据类型(局部变量)</p>
<p>堆: 存放对象，数组</p>
<p>方法区：常量池(常量，比如字符串)，类加载信息</p>
<h4 id="14-1-属性"><a href="#14-1-属性" class="headerlink" title="14.1 属性"></a>14.1 属性</h4><blockquote>
<p>从概念叫法上看，成员变量 = 属性 。</p>
</blockquote>
<p>属性是类的一个组成部分，一般是基本数据类型，也可以是引用数据类型(对象，数组)。</p>
<p>属性的定义语法同变量：</p>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token comment">//访问修饰符 属性类型 属性名；</span>
<span class="token keyword">public</span> <span class="token keyword">int</span> age；<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre>

<p>控制属性的访问范围</p>
<ol>
<li>public </li>
<li>protected</li>
<li>默认</li>
<li>private</li>
</ol>
<p>属性可以定义为任何类型，包括基本类型和引用类型。</p>
<p>属性如果不赋值，有默认值，同数组。</p>
<ul>
<li>int 0   short 0 long 0 byte 0 float 0.0 double 0.0 char \u0000 boolean false String null</li>
</ul>
<h4 id="14-2-成员方法"><a href="#14-2-成员方法" class="headerlink" title="14.2 成员方法"></a>14.2 成员方法</h4><p>​    成员方法的定义：</p>
<ul>
<li>提高了代码复用性</li>
<li>可以将实现的细节封装起来，然后供其他的用户使用</li>
<li>成员方法中不能再定义方法</li>
</ul>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">class</span> clssname<span class="token punctuation">&#123;</span>
<span class="token class-name">String</span> name<span class="token punctuation">;</span>					<span class="token comment">//属性</span>
访问修饰符 返回数据类型 方法名<span class="token punctuation">(</span>参数列表……<span class="token punctuation">)</span><span class="token punctuation">&#123;</span>			 <span class="token comment">//方法=</span>
	语句；
<span class="token keyword">return</span> 返回值；
<span class="token punctuation">&#125;</span>
<span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>



<ol>
<li><p>访问修饰符控制方法的使用范围。</p>
</li>
<li><p>数据类型(返回类型)： 表示成员方法输出，void表示没有返回值。</p>
<ul>
<li>返回类型可以为任意类型，包含基本类型或引用类型，一个方法只有一个返回值。</li>
<li>return语句不是必须的 ，但是如果方法要求有返回的数据类型，则必须要有return语句且返回数据类型必须和return的值类型一致或者兼容。</li>
</ul>
</li>
<li><p>形参列表：表示成员方法的输入。</p>
<ul>
<li>一个方法可以有零个参数也可以有多个参数，中间用逗号隔开。</li>
<li>参数类型可以为任意类型，包含基本类型或者引用类型。</li>
<li>调用带参数的方法时，一定对应这参数列表传入相同类型或者兼容类型的参数。</li>
<li>方法定义时的参数被称为形式参数，简称形参，方法调用时的参数被称为实际参数，简称实参。实参和形参的类型要一致或者兼容，个数顺序必须一致。</li>
</ul>
</li>
</ol>
<p>方法调用小结：</p>
<ol>
<li>当程序执行到方法时就会开辟一个独立的空间(栈空间)。</li>
<li>当方法执行完毕或者执行到return时就会返回。</li>
<li>返回到调用方法的地方。</li>
<li>返回后，继续执行方法后面的代码。</li>
<li>当main方法(栈执行完毕，整个程序退出)。</li>
</ol>
<h4 id="14-3-方法的重载"><a href="#14-3-方法的重载" class="headerlink" title="14.3 方法的重载"></a>14.3 方法的重载</h4><blockquote>
<p>java中允许同一个类中，多个同名方法的存在，但是要求形参列表不一致(形参名字无所谓，主要是形参类型和顺序)。</p>
</blockquote>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">class</span> <span class="token class-name">Myclass</span><span class="token punctuation">&#123;</span>
  <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">myOverload</span><span class="token punctuation">(</span><span class="token keyword">int</span> i<span class="token punctuation">)</span><span class="token punctuation">&#123;</span>  <span class="token punctuation">&#125;</span>
  <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">myOverload</span><span class="token punctuation">(</span><span class="token keyword">char</span> name<span class="token punctuation">)</span><span class="token punctuation">&#123;</span>  <span class="token punctuation">&#125;</span>
  <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">myOverload</span><span class="token punctuation">(</span><span class="token class-name">String</span> age<span class="token punctuation">)</span><span class="token punctuation">&#123;</span>  <span class="token punctuation">&#125;</span>
<span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span></span></code></pre>



<h4 id="14-4-可变参数"><a href="#14-4-可变参数" class="headerlink" title="14.4 可变参数"></a>14.4 可变参数</h4><blockquote>
<p>java中允许将同一个类中多个同名同功能但是参数不同的方法封装为一个方法。</p>
</blockquote>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token comment">//基本语法：</span>
访问修饰符 返回类型 方法名<span class="token punctuation">(</span> 数据类型<span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span> 形参名 <span class="token punctuation">)</span><span class="token punctuation">&#123;</span>

<span class="token punctuation">&#125;</span>
<span class="token comment">//  int...表示接受的是可变参数，类型是int，使用可变参数可以当作数组使用</span>
<span class="token comment">//使用形参名.length可以得到输入参数的长度</span>
<span class="token keyword">class</span> <span class="token class-name">Myclass</span><span class="token punctuation">&#123;</span>
<span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">myOverload</span><span class="token punctuation">(</span><span class="token keyword">int</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span> nums<span class="token punctuation">)</span><span class="token punctuation">&#123;</span>  <span class="token punctuation">&#125;</span>

<span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>



<ul>
<li>可变参数的实参可以是0个也可以是任意多个。</li>
<li>可变参数的实参可以是数组。</li>
<li>可变参数的本质就是数组。</li>
<li>可变参数可以和普通参数一起放在形参列表，==但是可变参数必须放在最后==.</li>
</ul>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">class</span> <span class="token class-name">Myclass</span><span class="token punctuation">&#123;</span>
<span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">myOverload</span><span class="token punctuation">(</span><span class="token class-name">String</span> name<span class="token punctuation">,</span><span class="token keyword">int</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span> nums<span class="token punctuation">)</span><span class="token punctuation">&#123;</span>  <span class="token punctuation">&#125;</span>

<span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span></span></code></pre>

<ul>
<li>一个形参列表只能出现一个可变参数。</li>
</ul>
<h4 id="14-5-构造器"><a href="#14-5-构造器" class="headerlink" title="14.5 构造器"></a>14.5 构造器</h4><p>构造器又叫构造方法，==是类的一种特殊方法，可以在创建类的对象时传入参数，完成数据的初始化。==</p>
<p>创建对象时，先创建属性，然后赋默认值，再调用构造器进行初始化。</p>
<p>[修饰符] 方法名(形参列表)  {  方法体;  }</p>
<p>构造器的修饰符有可以默认，构造器没有返回值。</p>
<p>构造器的方法名必须和类名一致。</p>
<p>构造器由系统调用。</p>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">class</span> doubao<span class="token punctuation">&#123;</span>
<span class="token class-name">String</span> name<span class="token punctuation">;</span>
<span class="token keyword">int</span> age<span class="token punctuation">;</span>
<span class="token keyword">public</span> <span class="token function">doubao</span><span class="token punctuation">(</span><span class="token class-name">String</span> name<span class="token punctuation">)</span><span class="token punctuation">&#123;</span>		<span class="token comment">//构造器没有返回值</span>
<span class="token keyword">this</span><span class="token punctuation">.</span>name <span class="token operator">=</span> name<span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span>   
<span class="token keyword">public</span> <span class="token function">doubao</span><span class="token punctuation">(</span><span class="token keyword">int</span> age<span class="token punctuation">)</span><span class="token punctuation">&#123;</span>		<span class="token comment">//构造器没有返回值</span>
<span class="token keyword">this</span><span class="token punctuation">.</span>age <span class="token operator">=</span> age<span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span> 
<span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>如果用户没有定义构造器，系统会自动给类生成一个默认无参数的构造器(也叫默认构造器)。</p>
<p>==一旦自己定义了构造器，系统就不会生成默认构造器。==</p>
<h4 id="14-6-this关键字"><a href="#14-6-this关键字" class="headerlink" title="14.6 this关键字"></a>14.6 this关键字</h4><p>this 关键字用来指代当前调用this的对象。</p>
<p>this可以看作时指代对象的属性，保存着指代对象的地址。</p>
<p>this关键字可以用来访问本类的属性，方法，构造器。</p>
<p>this用于区分当前类的属性和局部变量。</p>
<p>访问成员方法的语法：this.方法名(参数列表)；</p>
<p>访问构造器语法this(参数列表)，==只能在构造器中使用。== ==且必须放在第一行==</p>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">class</span> doubao<span class="token punctuation">&#123;</span>
	<span class="token class-name">String</span> name<span class="token punctuation">;</span>
	<span class="token keyword">int</span> age<span class="token punctuation">;</span>
	<span class="token keyword">public</span> <span class="token function">doubao</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>		
	<span class="token keyword">this</span><span class="token punctuation">(</span><span class="token number">12</span><span class="token punctuation">,</span><span class="token string">"doubao"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>   <span class="token comment">//无参构造器访问了有参数构造器</span>
	<span class="token keyword">public</span> <span class="token function">doubao</span><span class="token punctuation">(</span><span class="token keyword">int</span> age<span class="token punctuation">,</span><span class="token class-name">String</span> name<span class="token punctuation">)</span><span class="token punctuation">&#123;</span>		<span class="token comment">//构造器没有返回值</span>
		<span class="token keyword">this</span><span class="token punctuation">.</span>age <span class="token operator">=</span> age<span class="token punctuation">;</span>
	<span class="token punctuation">&#125;</span> 
<span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>



<p>this不能再类定义的外部使用，只能再类中的方法中使用。</p>
<h3 id="15-递归"><a href="#15-递归" class="headerlink" title="15 递归"></a>15 递归</h3><blockquote>
<p>执行一个方法时，就创建一个新的相互独立的空间(栈空间);</p>
<p>方法的局部变量时独立的不会互相影响。</p>
<p>如果方法中使用的是引用类型的变量，就会共享引用类型变量的数据。</p>
<p>递归必须向推出递归条件逼近，否则就是无限递归。</p>
</blockquote>
<h3 id="16-作用域"><a href="#16-作用域" class="headerlink" title="16. 作用域"></a>16. 作用域</h3><p>作用域细节：</p>
<p>在Java中，主要的变量就是属性(成员变量)和局部变量。</p>
<p>局部变量一般是指在成员方法中定义的变量。</p>
<ul>
<li>全局变量：就是属性，作用域为整个类体。可以被本类使用，也可以在其他类使用。</li>
<li>局部变量：也就是属性之外的其他变量，作用于定义它的代码块中。</li>
</ul>
<p>==全局变量可以不赋值，直接使用，因为有默认值，局部变量必须赋值使用，没有默认值。==</p>
<p>全局变量在类的初始化阶段会被赋值，局部变量不会经历初始化阶段不会自动赋初值。</p>
<ul>
<li>属性和局部变量可以重名，访问遵循就近原则。</li>
<li>在同一个作用域中，比如在同一个成员方法中，两个局部变量，不能重名。</li>
<li>属性生命周期长，伴随着对象的创建而创建，伴随着对象的死亡而死亡，局部变量，生命周期短，伴随着他的代码块的执行而创建，伴随着代码块的结束而死亡。</li>
<li>==全局变量可以加访问修饰符，局部变量不能加访问修饰符==。</li>
</ul>
<h3 id="17-包"><a href="#17-包" class="headerlink" title="17. 包"></a>17. 包</h3><blockquote>
<p>本质：实际上就是创建不同的文件夹保存文件。</p>
<p>作用：区分相同名字的类，控制管理类。控制类的访问范围。</p>
</blockquote>
<p>命名规则：</p>
<p>​     只能包含数字，字母，下划线，小圆点，但是不能以数字开头，不能是关键字或者保留字。</p>
<p>命名规范：</p>
<p>​    一般是小写字母 + 小圆点，com.公司名.项目名.业务模块名s</p>
<pre class="line-numbers language-java" data-language="java"><code class="language-java">java<span class="token punctuation">.</span>lang<span class="token punctuation">.</span>*  基本包，默认引入，不需要再引入
java<span class="token punctuation">.</span>util<span class="token punctuation">.</span>*  util包，系统提供的工具包，工具类，使用<span class="token class-name">Scanner</span>
java<span class="token punctuation">.</span>net<span class="token punctuation">.</span>* 网络包，网络开发
java<span class="token punctuation">.</span>awt<span class="token punctuation">.</span>* 是做<span class="token class-name">Java</span>的界面开发。<span class="token constant">GUI</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span></span></code></pre>

<p>package 的作用是声明当前类所在的包，需要放在类的最上面，一个类中最多只有一句package。</p>
<p>import指令位置放在package的下面，再类定义前面，可以有多句没有顺序。</p>
<h3 id="18-访问修饰符"><a href="#18-访问修饰符" class="headerlink" title="18. 访问修饰符"></a>18. 访问修饰符</h3><blockquote>
<p>java一共提供了四种访问修饰符号，用于控制方法和属性(成员变量)的访问权限(范围)。</p>
</blockquote>
<p>1.公开级别： public，对外公开。</p>
<p>2.受保护级别：protected，对子类和同一个包中的类公开。</p>
<p>3.默认级别：没有符号，向同一个包的类公开。</p>
<p>4.私有级别：private修饰，只允许类本身可以访问，不公开。</p>
<p>注意事项：</p>
<ul>
<li>修饰符可以用来修饰类中的属性，成员方法以及类。</li>
<li>只有默认的和public才能修饰类，并且遵循以上的访问权限的特点。</li>
<li>成员方法的访问规则和属性完全一样。</li>
</ul>
<h3 id="19-面向对象—封装"><a href="#19-面向对象—封装" class="headerlink" title="19. 面向对象—封装"></a>19. 面向对象—封装</h3><blockquote>
<p>面向对象三大特征：封装，继承，多态。</p>
</blockquote>
<p>封装：就是把抽象出来的对象[属性]和对数据的操作[方法]封装一起，数据被保护在内部程序的其他部分只有通过被授权的操作[方法]，才能对数据进行操作。</p>
<ul>
<li>隐藏实现细节。</li>
<li>可以对数据进行验证，保证安全合理。</li>
</ul>
<p>封装实现步骤：</p>
<ol>
<li>将属性进行私有化。</li>
<li>提供一个公共的set方法，用于对属性赋值。</li>
<li>提供一个公共的get方法，用于获取属性的值。</li>
</ol>
<h3 id="20-面向对象—继承"><a href="#20-面向对象—继承" class="headerlink" title="20. 面向对象—继承"></a>20. 面向对象—继承</h3><blockquote>
<p> 继承可以解决代码复用，让我们的编程更加接近人类思维，当多个类存在相同的属性(变量)和方法时，可以从这些类中抽象出父类，在父类中定义相同的属性和方法，所有的子类不许要重新定义这些方法和属性，只需要通过extends来声明继承父类即可。</p>
</blockquote>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">class</span> 子类 <span class="token keyword">extends</span> 父类<span class="token punctuation">&#123;</span>

<span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span></span></code></pre>

<p>子类自动拥有父类定义的属性和方法。</p>
<p>父类又叫超类，基类。</p>
<p>子类又叫派生类。</p>
<p>继承细节：</p>
<ul>
<li>子类继承了所有的属性和方法，但是私有属性和方法不能在子类中直接访问，要通过父类提供的公共的方法访问。(继承包括私有的所有属性方法只是不能访问)</li>
<li>子类必须调用父类的构造器完成父类的初始化。</li>
<li>当创建子类对象时，不管使用子类的哪个构造器，默认情况下总会去调用父类的无参构造器，如果父类没有提供无参构造器，则必须在子类的构造器中用super去指定使用父类的哪个构造器完成对父类的初始化工作，否则编译不会通过。</li>
<li>如果希望指定去调用父类的某个构造器，则显示的调用一下。</li>
</ul>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">super</span><span class="token punctuation">(</span>参数列表<span class="token punctuation">)</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>



<ul>
<li>super在使用时，需要放在构造器的第一行。</li>
<li>super()和this()都只能放在构造器的第一行，因此这两个方法不能共存在一个构造器中。</li>
<li>java的所有类都是object的子类。</li>
<li>父类构造器的调用不限于直接父类，将一直追溯到object类。</li>
<li>子类最多只能直接继承一个父类。</li>
<li>不能滥用继承，子类和父类之间必须满足is-a的逻辑关系。</li>
</ul>
<h4 id="super关键字"><a href="#super关键字" class="headerlink" title="super关键字"></a>super关键字</h4><blockquote>
<p>super代表父类的引用，用于访问父类属性，方法，构造器。</p>
</blockquote>
<ol>
<li>访问父类的属性，但是不能访问父类的private属性。</li>
</ol>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">super</span><span class="token punctuation">.</span>属性名；<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>

<ol start="2">
<li>访问父类的方法，==不能访问父类的private方法==。</li>
</ol>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">super</span><span class="token punctuation">.</span>方法名<span class="token punctuation">(</span>参数列表<span class="token punctuation">)</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>

<ol start="3">
<li>访问父类的构造器。</li>
</ol>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">super</span><span class="token punctuation">(</span>参数列表<span class="token punctuation">)</span><span class="token punctuation">;</span>   <span class="token comment">//只能放在构造器的第一句，只能出现一句。</span><span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>



<table>
<thead>
<tr>
<th>区别点</th>
<th>this</th>
<th>super</th>
<th></th>
</tr>
</thead>
<tbody><tr>
<td>访问属性</td>
<td>访问本类中的属性，==如果本类中没有此属性则从父类中继续查找==</td>
<td>访问父类中的属性</td>
<td></td>
</tr>
<tr>
<td>调用方法</td>
<td>访问本类中的方法，如果本类中没有此方法则从父类继续查找</td>
<td>直接访问父类中的方法</td>
<td></td>
</tr>
<tr>
<td>调用构造器</td>
<td>调用本类构造器，必须放在构造器的首行</td>
<td>调用父类构造器，必须放在子类构造器的首行</td>
<td></td>
</tr>
<tr>
<td>特殊</td>
<td>表示当前对象</td>
<td>子类中访问父类对象</td>
<td></td>
</tr>
</tbody></table>
<h3 id="21-重写-override"><a href="#21-重写-override" class="headerlink" title="21. 重写(override)"></a>21. 重写(override)</h3><blockquote>
<p>子类 有一个方法，和父类的某个方法的名称，返回类型，参数是一样的，那么我们可以说这个方法覆盖了父类的方法。</p>
</blockquote>
<p>方法重写细节：</p>
<ol>
<li><p>子类方法的参数，方法名称，要和父类方法的参数，方法名称完全一样。</p>
</li>
<li><p>子类方法的返回类型和父类方法的返回类型一样，或者父类是返回类型的子类。</p>
</li>
</ol>
<p>例如：父类返回类型：object    子类返回类型：  String  </p>
<ol start="3">
<li>子类方法不能缩小父类方法的访问权限。</li>
</ol>
<p>注意：子类与父类中同名同参数的方法必须同时声明为非static的(即为重写)，或者同时声明为static的（不是重写） 。因为static方法是属于类的，子类无法覆盖父类的方法。 </p>
<table>
<thead>
<tr>
<th>名称</th>
<th>发生范围</th>
<th>方法名</th>
<th>参数列表</th>
<th>返回类型</th>
<th>修饰符</th>
</tr>
</thead>
<tbody><tr>
<td>重载</td>
<td>本类</td>
<td>必须一样</td>
<td>类型，个数，顺序至少一个不同</td>
<td>无要求</td>
<td>无要求</td>
</tr>
<tr>
<td>重写</td>
<td>父子类</td>
<td>必须一样</td>
<td>必须完全相同</td>
<td>子类重写的方法和父类返回的类型一致或者是其子类</td>
<td>子类方法不能缩小父类的访问范围</td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
</tbody></table>
<h3 id="22-面向对象—多态"><a href="#22-面向对象—多态" class="headerlink" title="22.面向对象—多态"></a>22.面向对象—多态</h3><blockquote>
<p>多态：方法或对象具有多种形态，多态是建立在封装和继承基础之上的。</p>
<p>多态有利于代码维护和代码复用性。</p>
</blockquote>
<h4 id="22-1-方法的多态"><a href="#22-1-方法的多态" class="headerlink" title="22.1 方法的多态"></a>22.1 方法的多态</h4><blockquote>
<p>方法的多态：重载：传入不同的参数会调用不同的方法。</p>
</blockquote>
<p>​                     重写：自动定位使用的方法。</p>
<h4 id="22-2-对象的多态："><a href="#22-2-对象的多态：" class="headerlink" title="22.2 对象的多态："></a>22.2 对象的多态：</h4><ul>
<li>一个==对象的编译类型和运行类型可以不一致==。</li>
<li>instanceof用于判断对象的==运行类型==是否为指定类型或者指定类型的子类型。</li>
<li>编译类型在定义对象时就确定了，不能改变。</li>
<li>运行类型是可以变化的。</li>
<li>编译类型看定义时 = 号的左边，运行类型看定义时 = 号的右边 。</li>
<li>==属性没有重写之说，属性的值看编译类型。==</li>
<li>instanceof判断的是运行类型。</li>
</ul>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token class-name">Animal</span> animal <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Dag</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>





<p>多态的使用前提： 两个对象之间存在继承关系。</p>
<p>本质：父类的引用指向了子类的对象(多态向上转型)</p>
<p>多态的向上转型：</p>
<blockquote>
<p>语法： 父类类型  引用名 = new 子类类型()</p>
</blockquote>
<p>特点： ==可以调用父类中的所有成员(需要遵循访问权限)，不能调用子类中的特有成员，最终结果看子类的具体实现。==   （在编译阶段，能和调用哪些成员由编译器决定）</p>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token class-name">Animal</span> animal <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Dag</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>



<p>多态的向下转型：</p>
<p>语法： 子类类型 引用名 = (子类类型)父类引用；</p>
<p>只能强转父类的引用，不能强转父类的对象。(改变的只是编译器所限定的调用范围 )</p>
<p>要求父类的引用必须指向的是当前目标类型的对象。</p>
<p>向下转型后可以调用子类类型中所有的成员。</p>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token class-name">Animal</span> animal <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Dag</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token class-name">Dag</span> dag <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token class-name">Dag</span><span class="token punctuation">)</span>animal<span class="token punctuation">;</span>      <span class="token comment">//可以使用子类全部方法</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre>

<h4 id="22-3-动态绑定机制"><a href="#22-3-动态绑定机制" class="headerlink" title="22.3 动态绑定机制"></a>22.3 动态绑定机制</h4><blockquote>
<p>==当调用对象的方法时，该方法会和该对象的内存地址/运行类型绑定==</p>
</blockquote>
<p>当调配用对象属性时，没有动态绑定机制，哪里声明，哪里使用。</p>
<p>发挥作用的顺序:  动态绑定机制    继承机制</p>
<h4 id="22-4-多态数组"><a href="#22-4-多态数组" class="headerlink" title="22.4 多态数组"></a>22.4 多态数组</h4><blockquote>
<p>可以将数组编译类型定义为父类类型，向数组中添加多种子类类型</p>
</blockquote>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token class-name">Object</span><span class="token punctuation">[</span><span class="token punctuation">]</span> obj <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Object</span><span class="token punctuation">[</span><span class="token number">5</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
obj<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Object</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
obj<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Ingter</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span></span></code></pre>



<h4 id="22-5-多态参数"><a href="#22-5-多态参数" class="headerlink" title="22.5 多态参数"></a>22.5 多态参数</h4><blockquote>
<p>方法定义的形参类型为父类类型，实参类型允许为子类类型。</p>
</blockquote>
<h3 id="23-Object类方法"><a href="#23-Object类方法" class="headerlink" title="23. Object类方法"></a>23. Object类方法</h3><h4 id="23-1-equals-与"><a href="#23-1-equals-与" class="headerlink" title="23.1 equals()与 =="></a>23.1 equals()与 ==</h4><p>== :即可以判断基本类型，又可以判断引用类型。</p>
<p>==: 如果判断基本类型，判断的是值是否相等。</p>
<p>==:如果判断引用类型，判断的是地址是否相等，即是不是同一个对象。（无论是否向上转型，只要地址相同就返回true）</p>
<p>equals: 是Object的方法，只能判断引用类型。</p>
<p>用于判断地址是否相等，子类中往往重写该方法，用于判断内容是否相等。</p>
<ul>
<li>Object中equals()判断地址是否相同。</li>
<li>其他重写后的equals()方法比较两个值是否相同。</li>
</ul>
<pre class="line-numbers language-java" data-language="java"><code class="language-java">object<span class="token punctuation">.</span><span class="token function">equals</span><span class="token punctuation">(</span>obj<span class="token punctuation">)</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>





<h4 id="23-2-hashCode"><a href="#23-2-hashCode" class="headerlink" title="23.2 hashCode"></a>23.2 hashCode</h4><blockquote>
<p>返回对象的哈希码值，支持此方法是为了提高哈希表的性能。(一般是通过该内部地址转换成一个整数来实现的)</p>
</blockquote>
<p>两个引用，如果指向的是同一个对象，则哈希值肯定是一样的！</p>
<p>两个引用，如果指向的不是同一个对象，则哈希值是不一样的。</p>
<p>哈希值主要是根据地址号来的，不能完全将哈希值等价于地址。</p>
<p>hashCode()如果需要，也可以重写。使相同的类的对象返回hashCode码相同。</p>
<pre class="line-numbers language-java" data-language="java"><code class="language-java">object<span class="token punctuation">.</span><span class="token function">hashCode</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>





<h4 id="23-3-toString"><a href="#23-3-toString" class="headerlink" title="23.3 toString()"></a>23.3 toString()</h4><p>默认返回： 全类名(包名 + 类名 ) + @ + 哈希值的十六进制</p>
<p>子类往往会重写toString()方法来返回对象的属性信息。</p>
<p>当直接输出一个对象时，toString()会默认被调用。</p>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">public</span> <span class="token class-name">String</span> <span class="token function">toString</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
        <span class="token keyword">return</span> <span class="token function">getClass</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">getName</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">+</span> <span class="token string">"@"</span> <span class="token operator">+</span> <span class="token class-name">Integer</span><span class="token punctuation">.</span><span class="token function">toHexString</span><span class="token punctuation">(</span><span class="token function">hashCode</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">&#125;</span>
object<span class="token punctuation">.</span><span class="token function">toString</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span></span></code></pre>



<h4 id="23-4-finalize"><a href="#23-4-finalize" class="headerlink" title="23.4 finalize()"></a>23.4 finalize()</h4><p>当对象被回收时，系统自动调用该对象的finalize()方法，默认处理。子类可以重写该方法，做一些释放资源的操作。</p>
<p>回收条件：当某个对象没有任何引用，则jvm就会认为这个对象是一个垃圾对象，就会使用垃圾回收机制来销毁该对象，在销毁该对象前，会先调用finelize()方法。可以重写finalize()方法，释放资源。</p>
<p>垃圾回收机制的调用，是由系统来决定，也可以通过System.gc()主动触发垃圾回收机制。</p>
<p>对象逃离死亡的最后机会。</p>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token class-name">System</span><span class="token punctuation">.</span><span class="token function">cg</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>



<h3 id="24-类变量和类方法-static"><a href="#24-类变量和类方法-static" class="headerlink" title="24. 类变量和类方法(static)"></a>24. 类变量和类方法(static)</h3><p>类变量又叫静态变量，该变量会被该类所有实例对象共享。</p>
<p>静态变量在类加载的时候就生成了，即使没有创建对象也可以使用。</p>
<p>定义类变量：</p>
<p>访问修饰符 static 数据类型 变量名；</p>
<p>static 访问修饰符 数据类型 变量名；</p>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">class</span> xxx<span class="token punctuation">&#123;</span>
<span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">int</span> count<span class="token punctuation">;</span>
<span class="token keyword">static</span> <span class="token keyword">public</span> <span class="token keyword">int</span> count
<span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span></span></code></pre>



<p>访问变量：        —–类变量的访问，必须遵循相关的访问权限。</p>
<p>对象名.类变量名；</p>
<p>类名.类变量名；     </p>
<p>类方法也叫静态方法。</p>
<p>不创建实例也可以调用方法(当作工具使用),使用静态方法。</p>
<p>定义静态方法：</p>
<p>访问修饰符 static 返回数据类型 方法名；</p>
<p>static 访问修饰符 返回数据类型 方法名；</p>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> 方法名<span class="token punctuation">&#123;</span>

<span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span></span></code></pre>

<p>访问方法：        —–类方法的访问，必须遵循相关的访问权限。</p>
<p>对象名.方法名；</p>
<p>类名.方法名； </p>
<p>注意事项：</p>
<ul>
<li>类方法和普通方法都是随着类的加载而加载，将结构信息存储在方法区，类方法中无this参数，普通方法隐含着this参数。</li>
<li>类方法不能使用和对象相关的关键字。</li>
<li>类方法可以通过类名调用，也可以通过对象名调用。</li>
<li>普通方法和对象有关，需要通过对象名调用。不能通过类名调用。</li>
<li>普通成员方法，既可以访问非静态成员，也可以访问静态成员。</li>
<li>==静态成员方法，只能访问静态成员==！</li>
</ul>
<h3 id="25-main方法"><a href="#25-main方法" class="headerlink" title="25. main方法"></a>25. main方法</h3><ol>
<li>java虚拟机需要调用类的main()方法，所以该方法的访问权限必须时public。</li>
<li>java虚拟机在执行main方法时不创建对象，所以该方法必须是static</li>
<li>该方法接收Sting类型的数组参数，该数组中保存着执行java命令时传递给所运行的类的参数。</li>
</ol>
<h3 id="26-代码块"><a href="#26-代码块" class="headerlink" title="26.代码块"></a>26.代码块</h3><blockquote>
<p>代码块又称初始化块，属于类中的成员[即类中的一部分]，类似方法，将逻辑语句封装在方法体中，通过{}包围起来。</p>
</blockquote>
<p>但和方法不同，没有方法名，没有返回，没有参数，只有方法体，而且不用通过对象或者类显示调用，而是==加载类时，或者创建对象时隐式==调用。</p>
<p>先调用代码块，后调用构造器。</p>
<p>定义语法：</p>
<p>[修饰符]{</p>
<p>​    代码</p>
<p>}</p>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">static</span><span class="token punctuation">&#123;</span>
代码；
<span class="token punctuation">&#125;</span>
<span class="token punctuation">&#123;</span>
代码；
<span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>



<ul>
<li>修饰符可选，只能填写static</li>
<li>代码块分为静态代码块和非静态代码块</li>
<li>逻辑语句可以为任何逻辑语句。(输入，输出，方法调用，循环，判断)</li>
<li>； 号可以写上也可以省略。</li>
</ul>
<p>代码块细节：</p>
<ol>
<li><p>static代码块也叫静态代码块，作用就是对类进行初始化，而且它随着类的加载而加载，并且==只会执行一次(类只加载一次)==。如果时普通代码块，每创建一个对象就执行一次。</p>
</li>
<li><p>类加载：</p>
<ol>
<li>创建对象实例</li>
<li>创建子类对象实例，父类也会加载</li>
<li>使用类的静态成员</li>
</ol>
</li>
<li><p>普通代码块在创建对象实例时，会隐式调用，被创建一次就调用一次，如果只是使用类的静态成员，普通代码块不会执行。</p>
</li>
<li><p>创建一个对象时，在一个类的调用顺序：</p>
<ol>
<li>调用静态代码块和静态属性初始化(注意：静态代码块和静态属性初始化调用的优先级一样，如果有多个静态代码块和多个静态变量初始化，则按照他们定义的顺序调用)</li>
<li>调用普通代码块和普通属性的初始化(注意：普通代码块和普通属性初始化调用的优先级一样，如果有多个普通代码块和多个普通属性初始化，则按照定义顺序调用。)</li>
<li>调用构造方法</li>
</ol>
</li>
<li><p>构造方法的前面其实隐含了super()和调用普通代码块。静态相关的代码块，属性初始化，在类加载时，就执行完毕，因此是优于构造器和普通代码块执行。</p>
</li>
</ol>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">public</span> 构造器<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
 <span class="token keyword">super</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
 普通代码块<span class="token punctuation">;</span>
 <span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span></span></code></pre>

<ol start="6">
<li><p>静态代码块，静态属性，普通代码块，普通属性初始化，构造方法的调用顺序：</p>
<ol>
<li>父类的静态代码块和静态属性</li>
<li>子类的静态代码块和静态属性</li>
<li>父类的普通代码块和普通属性初始化</li>
<li>父类的构造方法</li>
<li>子类的普通代码块和普通属性初始化</li>
<li>子类的构造方法</li>
</ol>
</li>
<li><p>静态代码块只能调用静态成员，普通代码块可以调用任意成员。</p>
</li>
</ol>
<h3 id="27-final关键字"><a href="#27-final关键字" class="headerlink" title="27. final关键字"></a>27. final关键字</h3><blockquote>
<p>final可以修饰类，属性，方法，和局部变量。</p>
</blockquote>
<p>final的使用场景</p>
<ol>
<li>不希望类被继承时，可以使用final修饰。</li>
</ol>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">final</span> <span class="token keyword">class</span> <span class="token class-name">A</span> <span class="token punctuation">&#123;</span>

<span class="token punctuation">&#125;</span> <span class="token comment">//这个类将无法被继承</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span></span></code></pre>

<ol start="2">
<li>不希望父类的某个方法被重写/覆盖时可以使用final关键字修饰。</li>
</ol>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">class</span> <span class="token class-name">A</span> <span class="token punctuation">&#123;</span>
<span class="token keyword">public</span> <span class="token keyword">final</span> <span class="token keyword">void</span> <span class="token function">hi</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>  <span class="token comment">//此方法不能被修改</span>

<span class="token punctuation">&#125;</span>
<span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span></span></code></pre>



<ol start="3">
<li>不希望类的某个属性的值被修改可以使用final修饰。</li>
</ol>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">class</span> <span class="token class-name">A</span> <span class="token punctuation">&#123;</span>
<span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">hi</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>  <span class="token comment">//此方法不能被修改</span>
	<span class="token keyword">public</span> <span class="token keyword">final</span> <span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">0.1</span><span class="token punctuation">;</span> <span class="token comment">//此属性不能被修改</span>
<span class="token punctuation">&#125;</span>
<span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span></span></code></pre>



<ol start="4">
<li>不希望某个局部变量被修改，可以使用final修饰。</li>
</ol>
<p>使用细节：</p>
<ul>
<li><p>final修饰的属性又叫常量，一般使用大写来命名。</p>
</li>
<li><p>final修饰的属性在定义时，必须赋初值，且以后不允许被修改，赋值可以在的位置：</p>
<ul>
<li>定义时： final int I = x；</li>
<li>构造器中</li>
<li>代码块中</li>
</ul>
</li>
<li><p>如果final修饰的属性时静态的，则初始化位置只能在</p>
<ul>
<li>定义时</li>
<li>静态代码块中</li>
</ul>
</li>
<li><p>final类不能继承，但是可以实例化对象</p>
</li>
<li><p>如果类不是final类。但是含有final方法，则该类虽然不能重写但是可以被继承。</p>
</li>
<li><p>一般来说，一个类已经是final，就没必要将方法修饰成final方法了。</p>
</li>
<li><p>final不能修饰构造方法。</p>
</li>
<li><p>final和sitatic往往搭配使用，效率更高，底层编译器做了优化处理。</p>
</li>
</ul>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">int</span> cun <span class="token operator">=</span> <span class="token number">1</span>； <span class="token comment">//只是用static修饰属性，使用时需要加载类</span>
<span class="token keyword">public</span> <span class="token keyword">final</span> <span class="token keyword">static</span> <span class="token keyword">int</span> num <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span>   <span class="token comment">//将final和static一起使用，不需要加载类，直接加载属性</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre>

<ul>
<li>包装类(integer,Double,Float,Boolean等都是final)，String也是final类。</li>
</ul>
<h3 id="28-抽象类"><a href="#28-抽象类" class="headerlink" title="28. 抽象类"></a>28. 抽象类</h3><p>使用abstract关键字修饰一个类时，这个类就是抽象类。</p>
<p>抽象类不能被实例化。</p>
<p>使用abstract关键字来修饰一个方法时，这个方法就是抽象方法。</p>
<p>抽象方法不能有方法体。</p>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">abstract</span> <span class="token keyword">class</span> cat<span class="token punctuation">&#123;</span>
	<span class="token keyword">public</span> <span class="token keyword">abstract</span> <span class="token keyword">void</span> <span class="token function">getInt</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span></span></code></pre>

<p>抽象类的价值更多作用在设计，设计者设计好后，让子类继承并实现抽象类。</p>
<p>抽象类细节：</p>
<ol>
<li>抽象类不能被实例化。</li>
<li>抽象类可以不包含抽象方法。</li>
<li>一旦一个类包含了抽象方法，则必须声明为从抽象类。</li>
<li>abstract只能修饰类和方法，不能修饰属性。</li>
<li>抽象类可以有任意成员(非抽象方法，构造器，静态属性)。</li>
<li>如果一个类继承了抽象类，则它必须实现(只要有方法体不管为不为空)抽象类的所有抽象方法，除非它自己也声明为抽象类。</li>
<li>抽象方法不能使用private，final，static来修饰，因为这些关键字都是和重写相违背的。</li>
</ol>
<h3 id="29-接口"><a href="#29-接口" class="headerlink" title="29. 接口"></a>29. 接口</h3><p>接口就是给出一些没有实现的方法，封装到一起，到某个要使用的时候。就根据具体情况把这些方法写出来。</p>
<p>在接口中可以省略abstract</p>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">interface</span> 接口名<span class="token punctuation">&#123;</span>
属性；
方法<span class="token punctuation">;</span>    <span class="token comment">//1.抽象方法   2.默认实现方法   3.静态方法</span>
<span class="token punctuation">&#125;</span>
<span class="token keyword">class</span> 类名 <span class="token keyword">implements</span> 接口<span class="token punctuation">&#123;</span>
实现的方法；
<span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<ul>
<li>接口在jdk7.0之前接口中所有方法都没有方法体，即都是抽象方法。</li>
<li>在jdk8.0及以后可以定义静态方法，和已经实现的方法，但是必须使用default修饰。</li>
</ul>
<p>接口的细节：</p>
<ul>
<li><p>接口不能被实例化，</p>
</li>
<li><p>接口中的方法都是public修饰的方法，在接口中的抽象方法可以省略abstract。</p>
</li>
<li><p>一个普通类实现接口，就要把接口中的所有抽象方法都实现。</p>
</li>
<li><p>抽象类实现接口可以不用全部实现接口的抽象方法。</p>
</li>
<li><p>一个类可以同时实现多个接口。</p>
</li>
<li><p>==接口中的属性只能是final，而且是public static final 修饰符==</p>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">int</span> a <span class="token operator">=</span> <span class="token number">1</span> <span class="token comment">//实际上是 public static final interesting a = 1；</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre></li>
<li><p>==接口中的属性的访问方式： 接口名.属性名。==</p>
</li>
<li><p>一个接口不能继承其他的类，但是可以继承多个别的接口。</p>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">interface</span> 接口名 <span class="token keyword">extends</span> 接口，接口；<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre></li>
<li><p>接口的修饰符只能是public和默认，这点和类相同。</p>
</li>
</ul>
<h4 id="接口与抽象类的区别"><a href="#接口与抽象类的区别" class="headerlink" title="接口与抽象类的区别"></a>接口与抽象类的区别</h4><ol>
<li>接口和继承所解决的问题不同：<ol>
<li>继承的价值主要在于：解决代码的复用性和可维护性。</li>
<li>接口的价值主要在于：设计，设计好各种规范(方法)，让其他类去实现这些方法。</li>
</ol>
</li>
<li>接口比继承更加灵活。</li>
<li>接口在一等程度上实现代码解耦。</li>
</ol>
<h4 id="接口的多态特性"><a href="#接口的多态特性" class="headerlink" title="接口的多态特性"></a>接口的多态特性</h4><ol>
<li>多态参数。 接口参数可以接收实现了接口的实例。</li>
<li>多态数组。 可以定义接口类型的数组，存储实现接口的对象。</li>
<li>接口存在多态传递现象。</li>
</ol>
<ul>
<li>若一个接口中定义了一个默认方法，而另外一个接口中也定义了一个同名同参数的方法（不管此方法是否是默认方法），在实现类同时实现了这两个接口时，会出现： 接口冲突。解决办法：实现类必须覆盖接口中同名同参数的方法，来解决冲突。</li>
<li>若一个接口中定义了一个默认方法，而父类中也定义了一个同名同参数的非抽象方法，则不会出现冲突问题。因为此时遵守： 类优先原则。 接口中具有相同名称和参数的默认方法会被忽略。 </li>
</ul>
<h3 id="30-内部类"><a href="#30-内部类" class="headerlink" title="30. 内部类"></a>30. 内部类</h3><p>一个类的内部又完整的嵌套了另一个类结构，被嵌套的类被称为内部类。嵌套其它类的类被称为外部类，这是类的的第五大成员。(属性，方法，构造器，代码块，内部类)</p>
<p>内部类的最大特点就是可以直接访问私有属性，并且可以体现类于类之间的包含关系。</p>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">class</span> 外部类<span class="token punctuation">&#123;</span>
<span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre>

<p>内部类分类：</p>
<ol>
<li>定义在外部类局部位置上(比如方法内，代码块中)<ol>
<li>局部内部类(有类名)</li>
<li>匿名内部类(没有类名)</li>
</ol>
</li>
<li>定义在外部类的成员位置上：<ol>
<li>成员内部类(没用static修饰)</li>
<li>静态内部类(使用static修饰)</li>
</ol>
</li>
</ol>
<h4 id="30-1-局部内部类"><a href="#30-1-局部内部类" class="headerlink" title="30.1 局部内部类"></a>30.1 局部内部类</h4><blockquote>
<p> 局部内部类时定义在外部类的局部位置，比如方法中，代码块中。并且有类名。</p>
</blockquote>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">class</span> <span class="token class-name">Outer02</span> <span class="token punctuation">&#123;</span>
<span class="token keyword">private</span> <span class="token keyword">int</span> n1 <span class="token operator">=</span><span class="token number">100</span><span class="token punctuation">;</span>
<span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">m1</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
  <span class="token keyword">class</span> <span class="token class-name">Outer01</span><span class="token punctuation">&#123;</span>
      <span class="token keyword">private</span> <span class="token keyword">int</span> n2 <span class="token operator">=</span> <span class="token number">10</span><span class="token punctuation">;</span>
      <span class="token punctuation">&#125;</span>
  <span class="token punctuation">&#125;</span>
<span class="token punctuation">&#125;</span>
<span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<ul>
<li><p>可以直接访问外部类的所有成员，包括私有的。</p>
</li>
<li><p>不能添加访问修饰符，因为他的地位就是一个局部变量。局部变量是不能使用修饰符的，但可以使用final修饰，因为局部变量也可以使用final。(使用final禁止继承)</p>
</li>
<li><p>作用域：==仅仅在定义它的方法或代码块中。==</p>
</li>
<li><p>局部内部类访问外部类的成员：直接访问。</p>
</li>
<li><p>外部类访问局部内部类的成员： 创建对象再访问(需要在作用域中)</p>
</li>
<li><p>外部类不能直接访问局部内部类(局部内部类是一个局部变量)</p>
</li>
<li><p>如果外部类和局部内部类的成员重名时，默认遵循就近原则，如果想访问外部类的成员 可以使用 外部类名.this.成员去访问。（==外部类.this实质就是外部类的对象==）</p>
</li>
</ul>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">class</span> <span class="token class-name">Outer02</span> <span class="token punctuation">&#123;</span>
<span class="token keyword">private</span> <span class="token keyword">int</span> n1 <span class="token operator">=</span><span class="token number">100</span><span class="token punctuation">;</span>
<span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">m1</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
    <span class="token keyword">class</span> <span class="token class-name">Outer01</span><span class="token punctuation">&#123;</span>
        <span class="token keyword">private</span> <span class="token keyword">int</span> n2 <span class="token operator">=</span> <span class="token number">10</span><span class="token punctuation">;</span>
        <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">m2</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
            <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token char">'内部类的n1'</span><span class="token operator">+</span> n1 <span class="token operator">+</span> <span class="token char">'外部类的n1'</span><span class="token operator">+</span><span class="token class-name">Outter02</span><span class="token punctuation">.</span><span class="token keyword">this</span><span class="token punctuation">.</span>n1<span class="token punctuation">)</span>
        <span class="token punctuation">&#125;</span>
        <span class="token punctuation">&#125;</span>
    <span class="token punctuation">&#125;</span>
<span class="token punctuation">&#125;</span>
<span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>



<h4 id="30-2-匿名内部类"><a href="#30-2-匿名内部类" class="headerlink" title="30.2 匿名内部类"></a>30.2 匿名内部类</h4><p>本质是类，内部类，同时还是一个对象。</p>
<p>匿名内部类既是一个类的定义，同时它本身也是一个对象，因此从语法上看，它既有定义类的特征，也有创建对象的特征。</p>
<p>可以直接访问外部类的所有成员包含私有的。</p>
<p>不能添加访问修饰符，因为它的地位就是一个局部变量。</p>
<p>作用域： 仅仅是在定义他的方法块中。</p>
<p>该类没有指定的名字，系统自动分配名字。(外部类名$1…)</p>
<p>匿名内部类定义在外部类的局部位置，比如方法中并且没有类名。</p>
<p>匿名内部类可以实现普通类，接口，抽象类，简化编程。</p>
<p>语法：</p>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">new</span> 类名或接口<span class="token punctuation">(</span>参数列表<span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
类体；
<span class="token punctuation">&#125;</span>；
接口或类类型 对象名  <span class="token operator">=</span> <span class="token keyword">new</span> 类名或接口<span class="token punctuation">(</span>参数列表<span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
类体
<span class="token punctuation">&#125;</span>；
<span class="token comment">//底层代码</span>
 <span class="token number">1.</span> 实现指定接口或者类的方法
 <span class="token number">2.</span> 返回对象
 <span class="token number">3.</span> 销毁实现的类
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>匿名内部类访问外部类成员： 直接访问。</p>
<p>外部其他类不能访问匿名内部类(匿名内部类的地位就是一个局部变量)</p>
<p>如果外部类和内部类的成员重名时，内部类的访问遵循就近原则，如果想访问内部类的成员可以使用 外部类.this.成员 去访问。( 外部类.this 表示指定类的实例 )</p>
<p>匿名内部类可以简化开发。</p>
<p>匿名内部类的编译类型就是指定的类型，运行类型就是系统自动指定的类型，匿名内部类使用一次后就销毁。(实现的类无法使用，对象可以重复使用)</p>
<p>使用方法数量由编译类型限定，方法内容由运行类型限定。</p>
<p>匿名内部类实践：</p>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token comment">//定义接口和指定方法</span>
<span class="token keyword">interface</span> <span class="token class-name">Doubao</span><span class="token punctuation">&#123;</span>
<span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">show</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span>

<span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">f1</span><span class="token punctuation">(</span><span class="token class-name">Doubal</span> doubao<span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
doubao<span class="token punctuation">.</span><span class="token function">show</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span>

<span class="token comment">//使用方法</span>
<span class="token function">f1</span><span class="token punctuation">(</span>                             <span class="token comment">//直接向方法种传入匿名内部类。</span>
<span class="token keyword">new</span> <span class="token class-name">Double</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
<span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">show</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"匿名内部类重写方法"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span>
<span class="token punctuation">&#125;</span>
<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>



<h4 id="30-3-成员内部类"><a href="#30-3-成员内部类" class="headerlink" title="30.3 成员内部类"></a>30.3 成员内部类</h4><blockquote>
<p>成员内部类定义在外部类的成员位置，并且没有static修饰。</p>
</blockquote>
<p>可以直接访问外部类的所有成员，包含私有。</p>
<p>可以任意添加访问修饰符(public ，protected， 默认 ，private)，因为它就是类的一个成员。</p>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">class</span> <span class="token class-name">Doubao</span><span class="token punctuation">&#123;</span>
	<span class="token keyword">class</span> <span class="token class-name">Dahei</span><span class="token punctuation">&#123;</span>

	<span class="token punctuation">&#125;</span>
<span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>作用域： 和外部类的其他成员一样。</p>
<p>成员内部类访问外部类： 直接访问</p>
<p>外部类访问内部类： 创建对象再访问</p>
<p>外部其他类访问成员内部类：</p>
<pre><code>      1. 外部类.成员内部类 对象名 = 外部类对象.new 内部类();
</code></pre>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token comment">//定义内部类和外部类</span>
<span class="token keyword">class</span> <span class="token class-name">Doubao</span><span class="token punctuation">&#123;</span>
<span class="token keyword">class</span> <span class="token class-name">Dahei</span><span class="token punctuation">&#123;</span>

<span class="token punctuation">&#125;</span>
<span class="token punctuation">&#125;</span>
<span class="token comment">//使用内部类</span>
<span class="token class-name">Doubao</span> doubao <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Doubao</span><span class="token punctuation">(</span><span class="token punctuation">)</span>；

<span class="token class-name">Doubao<span class="token punctuation">.</span>Dahei</span> <span class="token operator">=</span> doubao<span class="token punctuation">.</span><span class="token keyword">new</span> <span class="token class-name">Dahei</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<ol start="2">
<li>在外部类中编写方法，返回内部类的实例。</li>
</ol>
<p>外部类和内部类的成员重名时，内部类访问的话，遵循就近原则，如果向访问外部类的成员可以使用外部类.this.成员 去访问。( 外部类.this 表示指定类的实例）</p>
<h4 id="30-4-静态内部类"><a href="#30-4-静态内部类" class="headerlink" title="30.4 静态内部类"></a>30.4 静态内部类</h4><blockquote>
<p>静态内部类是定义在外部类的成员位置，并且有static修饰。</p>
</blockquote>
<p>可以直接访问外部类的所有静态成员，包含私有的，但是不能直接访问非静态成员。</p>
<p>可以添加任意访问修饰福(public,protected,默认,private),因为它的地位就是一个成员。</p>
<p>作用域：同其他成员，为整个类体。</p>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token comment">//定义静态内部类和外部类</span>
<span class="token keyword">class</span> <span class="token class-name">Doubao</span><span class="token punctuation">&#123;</span>
<span class="token keyword">static</span> <span class="token keyword">class</span> <span class="token class-name">Dahei</span><span class="token punctuation">&#123;</span>

<span class="token punctuation">&#125;</span>
<span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>静态内部类访问外部类(比如静态属性)： 直接访问所有静态成员</p>
<p>外部类访问静态内部类 ： 创建对象再访问。</p>
<p>外部其他类访问静态内部类：</p>
<ol>
<li>外部类名.静态内部类名 对象名 = new 外部类名.静态内部类名();</li>
</ol>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token comment">//定义静态内部类和外部类</span>
<span class="token keyword">class</span> <span class="token class-name">Doubao</span><span class="token punctuation">&#123;</span>
<span class="token keyword">static</span> <span class="token keyword">class</span> <span class="token class-name">Dahei</span><span class="token punctuation">&#123;</span>
 
<span class="token punctuation">&#125;</span>
<span class="token punctuation">&#125;</span>
<span class="token comment">//使用静态内部类</span>
<span class="token class-name">Doubao<span class="token punctuation">.</span>Dahei</span> 对象名 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Doubao<span class="token punctuation">.</span>Dahei</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">//因为是静态内部类，是可以通过类名直接访问(前提满足访问权限)</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>2.编写方法，返回静态内部类实例。</p>
<p>如果外部类和静态内部类的成员重名，静态内部类访问的时候，默认遵循就近原则，如果向访问外部类的成员，则可以试用(外部类名.成员)去访问。</p>
<h3 id="31-枚举-eunmeration"><a href="#31-枚举-eunmeration" class="headerlink" title="31 枚举(eunmeration)"></a>31 枚举(eunmeration)</h3><blockquote>
<p>把具体对象一个一个列举。</p>
<p>枚举是一组常量的集合。可以理解为：枚举属于一种特殊的类，里面只包含一组有限的特定的对象。</p>
</blockquote>
<p>得到枚举类型的方法：</p>
<ol>
<li>自定义枚举类型</li>
<li>使用系统关键字得到枚举类型</li>
</ol>
<h4 id="31-1-自定义实现枚举类型"><a href="#31-1-自定义实现枚举类型" class="headerlink" title="31.1 自定义实现枚举类型"></a>31.1 自定义实现枚举类型</h4><p>不需要提供set方法，因此枚举对象值通常设置为只读。</p>
<p>枚举类的构造器是私有修饰符。</p>
<p>对枚举对象/属性使用final+static共同修饰，实现底层优化。</p>
<p>枚举对象名通常使用全部大写，常量的命名规范。</p>
<p>枚举根据需要，也可以有多个属性。</p>
<p>特点：</p>
<ul>
<li>构造器私有化</li>
<li>本类内部创建一组对象。</li>
<li>对外暴露对象(通过为对象添加public final static修饰符)</li>
<li>可以提供get方法，但是保护要提供set。</li>
</ul>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token number">1.</span>构造器私有化防止外部创建对象。
<span class="token number">2.</span>去掉set方法，防止属性被修改。
<span class="token number">3.</span>再枚举类内部直接创建固定的对象。
<span class="token number">4.</span>优化，加入<span class="token keyword">final</span>修饰符。
<span class="token keyword">class</span> <span class="token class-name">Season</span><span class="token punctuation">&#123;</span>
	<span class="token keyword">private</span> <span class="token class-name">String</span> name<span class="token punctuation">;</span>
    <span class="token keyword">private</span> <span class="token class-name">String</span> desc<span class="token punctuation">;</span><span class="token comment">//描述</span>

    <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">final</span> <span class="token class-name">Season</span> <span class="token constant">SPRING</span> <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Season</span><span class="token punctuation">(</span><span class="token string">"春天"</span><span class="token punctuation">,</span><span class="token string">"温暖"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">final</span> <span class="token class-name">Season</span> <span class="token constant">WINTER</span> <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Season</span><span class="token punctuation">(</span><span class="token string">"夏天"</span><span class="token punctuation">,</span><span class="token string">"温暖"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	
	<span class="token comment">//构造器需要私有，防止外部调用new创建对象。</span>
    <span class="token keyword">private</span> <span class="token class-name">Season</span><span class="token punctuation">(</span><span class="token class-name">String</span> name<span class="token punctuation">,</span> <span class="token class-name">String</span> desc<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
        <span class="token keyword">this</span><span class="token punctuation">.</span>name <span class="token operator">=</span> name<span class="token punctuation">;</span>
        <span class="token keyword">this</span><span class="token punctuation">.</span>desc <span class="token operator">=</span> desc<span class="token punctuation">;</span>
    <span class="token punctuation">&#125;</span>
<span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<h4 id="31-2-enum关键字实现枚举"><a href="#31-2-enum关键字实现枚举" class="headerlink" title="31.2 enum关键字实现枚举"></a>31.2 enum关键字实现枚举</h4><blockquote>
<p>使用enum关键字实现枚举类</p>
</blockquote>
<ol>
<li><p>使用关键字enum代替class</p>
</li>
<li><p>将public static final Season SPRING = new Season(“春天”,”温暖”);改为：</p>
</li>
</ol>
<p>SPRING(“春天”,”温暖”);  （ 枚举类对象名(参数列表) ）</p>
<ol start="3">
<li><p>如果有多个常量(对象)，使用 ， 间隔即可。</p>
</li>
<li><p>如果使用enum来实现，需要将常量定义语句现在最前面。</p>
</li>
<li><p>==使用enum关键字后不能继承其他类了==，因为enum会隐式继承Enum，而java是单继承机制。</p>
</li>
<li><p>枚举类和普通类一样，可以实现接口。</p>
</li>
</ol>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">enum</span>  <span class="token class-name">Season</span><span class="token punctuation">&#123;</span>
    <span class="token function">SPRING</span><span class="token punctuation">(</span><span class="token string">"春天"</span><span class="token punctuation">,</span><span class="token string">"温暖"</span><span class="token punctuation">)</span>，     <span class="token comment">//使用逗号间隔</span>
  	<span class="token function">WINTER</span><span class="token punctuation">(</span><span class="token string">"夏天"</span><span class="token punctuation">,</span><span class="token string">"温暖"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token keyword">private</span> <span class="token class-name">String</span> name<span class="token punctuation">;</span>
    <span class="token keyword">private</span> <span class="token class-name">String</span> desc<span class="token punctuation">;</span><span class="token comment">//描述</span>

<span class="token comment">//    public static final Season SPRING = new Season("春天","温暖");</span>
<span class="token comment">//    public static final Season  WINTER = new Season("夏天","温暖");</span>
	<span class="token comment">//构造器需要私有，防止外部调用new创建对象。</span>
    <span class="token keyword">private</span> <span class="token class-name">Season</span><span class="token punctuation">(</span><span class="token class-name">String</span> name<span class="token punctuation">,</span> <span class="token class-name">String</span> desc<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
        <span class="token keyword">this</span><span class="token punctuation">.</span>name <span class="token operator">=</span> name<span class="token punctuation">;</span>
        <span class="token keyword">this</span><span class="token punctuation">.</span>desc <span class="token operator">=</span> desc<span class="token punctuation">;</span>
    <span class="token punctuation">&#125;</span>

    <span class="token annotation punctuation">@Override</span>
    <span class="token keyword">public</span> <span class="token class-name">String</span> <span class="token function">toString</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
        <span class="token keyword">return</span> <span class="token string">"Season&#123;"</span> <span class="token operator">+</span>
                <span class="token string">"name='"</span> <span class="token operator">+</span> name <span class="token operator">+</span> <span class="token char">'\''</span> <span class="token operator">+</span>
                <span class="token string">", desc='"</span> <span class="token operator">+</span> desc <span class="token operator">+</span> <span class="token char">'\''</span> <span class="token operator">+</span>
                <span class="token char">'&#125;'</span><span class="token punctuation">;</span>
    <span class="token punctuation">&#125;</span>
<span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<ul>
<li>当我们使用enum关键字开发一个枚举类时，==默认会继承enum类==。</li>
<li>传统的public static dinal Season2 SPEING = new Seasonal(“春天”，“温暖”)；</li>
</ul>
<p>简化成SPRING(“春天”，“温暖”),这里必须知道，它调用的是哪个构造器。</p>
<ul>
<li>如果使用无参构造器创建枚举对象，则实参列表和小括号都可以省略。</li>
</ul>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">enum</span>  <span class="token class-name">Season</span><span class="token punctuation">&#123;</span>
<span class="token function">SPRING</span><span class="token punctuation">(</span><span class="token string">"春天"</span><span class="token punctuation">,</span><span class="token string">"温暖"</span><span class="token punctuation">)</span>，     <span class="token comment">//使用逗号间隔</span>
	<span class="token function">WINTER</span><span class="token punctuation">(</span><span class="token string">"夏天"</span><span class="token punctuation">,</span><span class="token string">"温暖"</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
	<span class="token constant">WUCAN</span><span class="token punctuation">;</span>   <span class="token comment">//等价于调用无参构造器WUCAN()</span>

<span class="token keyword">private</span> <span class="token class-name">String</span> name<span class="token punctuation">;</span>
<span class="token keyword">private</span> <span class="token class-name">String</span> desc<span class="token punctuation">;</span><span class="token comment">//描述</span>
<span class="token keyword">private</span> <span class="token class-name">Season</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
<span class="token punctuation">&#125;</span>
<span class="token keyword">private</span> <span class="token class-name">Season</span><span class="token punctuation">(</span><span class="token class-name">String</span> name<span class="token punctuation">,</span> <span class="token class-name">String</span> desc<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
    <span class="token keyword">this</span><span class="token punctuation">.</span>name <span class="token operator">=</span> name<span class="token punctuation">;</span>
    <span class="token keyword">this</span><span class="token punctuation">.</span>desc <span class="token operator">=</span> desc<span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span>
<span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<ul>
<li>枚举对象必须放在枚举类的首行。</li>
</ul>
<p>==使用enum关键字会隐式继承Enum类，这样就可以使用Enum类的相关的 方法。==</p>
<table>
<thead>
<tr>
<th>方法名</th>
<th>详细描述</th>
</tr>
</thead>
<tbody><tr>
<td>valueOf</td>
<td>传递给方法枚举常量的字符串，得到相应常量名的对象。</td>
</tr>
<tr>
<td>toString</td>
<td>得到当前枚举常量的的名称。你可以通过重写这个方法使得到的结果更加易读。</td>
</tr>
<tr>
<td>equals</td>
<td>再枚举类型中直接使用“==”判断枚举常量是否相等，equals为了在Set,List和Map中使用。equals()是不可变的。</td>
</tr>
<tr>
<td>hashCode</td>
<td>实现了hashCode和equals保持不变</td>
</tr>
<tr>
<td>getDeclanngClass</td>
<td>得到枚举常量所属枚举类型Class对象，可以用来判断两个枚举常量是否属于同一枚举类型。</td>
</tr>
<tr>
<td>name</td>
<td>得到当前枚举常量的名称。建议有限使用toString</td>
</tr>
<tr>
<td>ordinal</td>
<td>得到当前枚举常量的次序。从0开始编号。</td>
</tr>
<tr>
<td>compareTo</td>
<td>枚举类型实现Comparable接口，这样可以比较两个枚举常量==编号==大小</td>
</tr>
<tr>
<td>clone</td>
<td>枚举类型不能被Clone，为了防止子类实现克隆方法，Enum实现了一个仅抛出CloneNotSupportedExcrption异常的不变Clone()</td>
</tr>
<tr>
<td>values</td>
<td>得到对应枚举类中的所有对象</td>
</tr>
</tbody></table>
<h3 id="31-注解"><a href="#31-注解" class="headerlink" title="31*  注解"></a>31*  注解</h3><blockquote>
<p>注解(Annotation)，用于修饰解释包，类，方法，属性，构造器，局部变量等数据信息。</p>
</blockquote>
<p>和注释一样，注解不影响程序逻辑，但是注解可以被编译或者运行，相当于嵌入在代码中的补充信息。</p>
<p>在javaSE中，注解的使用目的比较简单，例如标记过时的功能，忽略警告等，在JavaEE中注解占据了更重要的角色，例如用来配置应用成秩序的任何切面，代替javaEE旧版中所遗留的繁荣代码和XML配置等。</p>
<p>使用Annotation时要在其前面增加@符号，并把该Annotation当成一个修饰符号使用，用于修饰它支持的程序元素</p>
<p>三个基本的Annotation：</p>
<ol>
<li><p>@Override：限定于某个方法，是重写父类方法，该注释只能用于方法。</p>
</li>
<li><p>@Deprecated：用于表示某个程序员元素(类，方法）已经过时。</p>
</li>
<li><p>@Suppress Warnings：抑制编译器警告。</p>
</li>
<li><p>@Target ：修饰注解的注解，被称为元注解。</p>
</li>
</ol>
<h4 id="31-1-Override"><a href="#31-1-Override" class="headerlink" title="31.1 @Override"></a>31.1 @Override</h4><blockquote>
<p>如果写了@Override注释，编译器就会去检查该方法是否真的重写父类的方法，如果的确重写了 ，则编译通过，如果没有构成重写，则编译错误。 </p>
</blockquote>
<p>@Override只能修饰方法。</p>
<h4 id="31-2-Deprecated"><a href="#31-2-Deprecated" class="headerlink" title="31.2 @Deprecated"></a>31.2 @Deprecated</h4><p>使用@Deprecated修饰某个元素，表示该元素已经过时</p>
<p>即不再推荐使用，但是仍然可以使用。</p>
<p>用于表示某个程序元素(类，方法等)已过时。</p>
<p>可以修饰方法，字段，包，参数，类等。</p>
<p>@Deprecated的作用可以做到新旧版本的兼容和过度。</p>
<h4 id="31-3-Suppress-Warnings"><a href="#31-3-Suppress-Warnings" class="headerlink" title="31.3 @Suppress Warnings"></a>31.3 @Suppress Warnings</h4><blockquote>
<p>当不希望看到警告时，可以使用@Suppress Warnings抑制警告信息。</p>
</blockquote>
<p>@Suppress Warnings({“警告信息”})</p>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token annotation punctuation">@Suppress</span> <span class="token class-name">Warnings</span><span class="token punctuation">(</span><span class="token punctuation">&#123;</span><span class="token string">"指定警告"</span><span class="token punctuation">&#125;</span><span class="token punctuation">)</span><span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>

<p>在方法前填写抑制范围为整个方法体。</p>
<p>在语句上填写一致范围为单独语句。</p>
<h4 id="31-4-元注解"><a href="#31-4-元注解" class="headerlink" title="31.4 元注解"></a>31.4 元注解</h4><blockquote>
<p>JDK的元注解用于修饰其他的Annotation，元注释就是修饰其他注释的注释。</p>
</blockquote>
<p>元注解的种类：</p>
<ol>
<li><p>Retention //指定注解的作用范围： SOURCE,CLASS,RUNTIME</p>
<ol>
<li>RetentionPolicy.SOURCE:编译器使用后，直接丢弃这种策略的注释。</li>
<li>RetentionPolicy.CLASS：编译器将把注解记录在class文件中，当运行Java程序时，JVM不会保留注释，这是默认值。</li>
<li>RetentionPolicy.RUNTIME:编译器将把注释记录在class文件中，当运行java、程序时，JVM会保留注解，程序可以通过反射获取该注释。</li>
</ol>
</li>
<li><p>Target  //指定注释可以在哪些地方使用。</p>
</li>
</ol>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token annotation punctuation">@Target</span><span class="token punctuation">(</span>value<span class="token operator">=</span><span class="token punctuation">&#123;</span>	<span class="token constant">CONSTRUCTOR</span><span class="token punctuation">,</span><span class="token constant">FIELD</span><span class="token punctuation">,</span><span class="token constant">LOCAL_VARIABLE</span><span class="token punctuation">,</span><span class="token constant">METHOD</span><span class="token punctuation">,</span><span class="token constant">PACKAGE</span><span class="token punctuation">,</span><span class="token constant">PARAMETER</span><span class="token punctuation">,</span><span class="token constant">TYPE</span><span class="token punctuation">&#125;</span><span class="token punctuation">)</span><span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>

<ol start="3">
<li><p>Documented  //指定该注释是否会在javadoc体现。</p>
</li>
<li><p>Inherited   //子类会继承父类注释。</p>
<ul>
<li>被它修饰的Annptation将具有继承性，如果某个类使用了被@inherited修饰的Annotation，则其子类将自动具有该注解。</li>
</ul>
</li>
</ol>
<h3 id="32-异常"><a href="#32-异常" class="headerlink" title="32. 异常"></a>32. 异常</h3><blockquote>
<p>将程序中发生的不正常情况称为“异常”，程序就会抛出异常 ：XXXXException</p>
</blockquote>
<p>可以使用  Ctrl + Alt + T 快捷处理</p>
<p>执行过程中发生的异常可以分为两类：</p>
<ul>
<li>Error(错误)： java虚拟机无法解决的严重问题。如：JVM系统内部错误，资源耗尽等严重情况。</li>
<li>Exception(异常):其他因为编程错误或者偶然的外在因素所导致的一般性问题，可以使用针对的代码进行处理，例如空指针访问，试图读取不存在的文件，网络链接中断。Exception分为两大类：运行时异常和编译异常。</li>
</ul>
<p>==当程序抛出异常后程序就退出，下面的代码就不再执行了。==</p>
<p>可以使用try-catch异常处理机制来捕获异常。保证程序的健壮性。</p>
<p>使用了异常处理机制。即使出现了异常，也会继续执行。（从出现异常的try块中语句跳转到catch或finally中，不会继续执行try块中的语句）</p>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">try</span><span class="token punctuation">&#123;</span>

<span class="token punctuation">&#125;</span><span class="token keyword">catch</span> <span class="token punctuation">&#123;</span>

<span class="token punctuation">&#125;</span>
<span class="token keyword">finally</span> <span class="token punctuation">&#123;</span>

<span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>



<h4 id="32-1-异常体系图"><a href="#32-1-异常体系图" class="headerlink" title="32.1 异常体系图"></a>32.1 异常体系图</h4><blockquote>
<p>所有异常的都继承了Serializable接口，继承了Object类</p>
</blockquote>
<p>[<a target="_blank" rel="noopener" href="https://imgtu.com/i/biOxl6"><img src="https://s4.ax1x.com/2022/02/24/biOxl6.md.png" alt="biOxl6.md.png"></a></p>
<ol>
<li>异常分为两大类，运行时异常和编译时异常。</li>
<li>运行时异常，编译器检测不出来。编译器不要求强制处理的异常，一般只是编程时的逻辑错误，是成员需要应该避免其出现的异常。java.long.RuntimeException类及他的子类都是运行时异常。</li>
<li>对于运行时异常，可以不做处理，因为这列异常很普遍，如果全处理可能会对程序的可读性和运行效率产生影响，</li>
<li>编译时异常，是编译器要求必须处理的异常。==方法抛出编译异常，使用方法时必须处理异常。==</li>
</ol>
<h4 id="31-2-常见的运行时异常"><a href="#31-2-常见的运行时异常" class="headerlink" title="31.2 常见的运行时异常"></a>31.2 常见的运行时异常</h4><ol>
<li>NullPointerException 空指针异常</li>
<li>ArithmeticException 数学运算异常</li>
<li>ArrayIndexOutOfBundsException 数组下标越界异常</li>
<li>ClassCastException 类型转换异常</li>
<li>NumberFormatException 字符串不能转换为数字</li>
</ol>
<h4 id="31-3-常见的编译异常"><a href="#31-3-常见的编译异常" class="headerlink" title="31.3 常见的编译异常"></a>31.3 常见的编译异常</h4><blockquote>
<p>编译异常是只在编译期间，就必须处理的异常，否则代码不能通过编译。</p>
</blockquote>
<p>SQLException 操作数据库时，查询表可能发生异常。</p>
<p>IOException 操作文件时，发生的异常。</p>
<p>FileNotFoundException 当操作一个不存在的文件时，发生异常</p>
<p>ClassNotFoundException 加载类，而该类不存在时，发生异常。</p>
<p>EOFException 操作文件，到文件末尾。发生异常。</p>
<p>IllegalArguementException 参数异常</p>
<h4 id="31-4-异常处理"><a href="#31-4-异常处理" class="headerlink" title="31.4 异常处理"></a>31.4 异常处理</h4><blockquote>
<p>异常处理就是当发生异常时，对异常处理的方式。</p>
</blockquote>
<ol>
<li>try—catch—finally 程序员在代码中捕获发生的异常，自行处理。</li>
<li>throws 将发生的异常抛出，交给调用者(方法)来处理，最顶级的处理就是JVM。(JVM的处理方法就是打印异常，终止程序)</li>
</ol>
<p>try—catch—finally </p>
<blockquote>
<p>java提供try和catch块来处理异常，try块用于包含可能出错的代码，catch块用与处理try块中发生的异常，可以根据需要在程序中有多个try- catch块。</p>
</blockquote>
<p>可以省略finally</p>
<p>细节：</p>
<ol>
<li><p>static代码块也叫静态代码块，作用就是对类进行初始化，而且它随着类的加载而加载，并且==只会执行一次(类只加载一次)==。如果时普通代码块，每创建一个对象就执行一次。</p>
</li>
<li><p>类加载：</p>
<ol>
<li>创建对象实例</li>
<li>创建子类对象实例，父类也会加载</li>
<li>使用类的静态成员</li>
</ol>
</li>
<li><p>普通代码块在创建对象实例时，会隐式调用，被创建一次就调用一次，如果只是使用类的静态成员，普通代码块不会执行。</p>
</li>
<li><p>创建一个对象时，在一个类的调用顺序：</p>
<ol>
<li>调用静态代码块和静态属性初始化(注意：静态代码块和静态属性初始化调用的优先级一样，如果有多个静态代码块和多个静态变量初始化，则按照他们定义的顺序调用)</li>
<li>调用普通代码块和普通属性的初始化(注意：普通代码块和普通属性初始化调用的优先级一样，如果有多个普通代码块和多个普通属性初始化，则按照定义顺序调用。)</li>
<li>调用构造方法</li>
</ol>
</li>
<li><p>构造方法的前面其实隐含了super()和调用普通代码块。静态相关的代码块，属性初始化，在类加载时，就执行完毕，因此是优于构造器和普通代码块执行。</p>
</li>
</ol>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">public</span> 构造器<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
 <span class="token keyword">super</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
 普通代码块<span class="token punctuation">;</span>
 <span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span></span></code></pre>

<ol start="6">
<li><p>静态代码块，静态属性，普通代码块，普通属性初始化，构造方法的调用顺序：</p>
<ol>
<li>父类的静态代码块和静态属性</li>
<li>子类的静态代码块和静态属性</li>
<li>父类的普通代码块和普通属性初始化</li>
<li>父类的构造方法</li>
<li>子类的普通代码块和普通属性初始化</li>
<li>子类的构造方法</li>
</ol>
</li>
<li><p>静态代码块只能调用静态成员，普通代码块可以调用任意成员。</p>
</li>
</ol>
<blockquote>
</blockquote>
<h4 id="31-3-常见的编译异常-1"><a href="#31-3-常见的编译异常-1" class="headerlink" title="31.3 常见的编译异常"></a>31.3 常见的编译异常</h4><blockquote>
<p>编译异常是只在编译期间，就必须处理的异常，否则代码不能通过编译。</p>
</blockquote>
<p>SQLException 操作数据库时，查询表可能发生异常。</p>
<p>IOException 操作文件时，发生的异常。</p>
<p>FileNotFoundException 当操作一个不存在的文件时，发生异常</p>
<p>ClassNotFoundException 加载类，而该类不存在时，发生异常。</p>
<p>EOFException 操作文件，到文件末尾。发生异常。</p>
<p>IllegalArguementException 参数异常</p>
<h4 id="31-4-异常处理-1"><a href="#31-4-异常处理-1" class="headerlink" title="31.4 异常处理"></a>31.4 异常处理</h4><blockquote>
<p>异常处理就是当发生异常时，对异常处理的方式。</p>
</blockquote>
<ol>
<li>try—catch—finally 程序员在代码中捕获发生的异常，自行处理。</li>
<li>throws 将发生的异常抛出，交给调用者(方法)来处理，最顶级的处理就是JVM。(JVM的处理方法就是打印异常，终止程序)</li>
</ol>
<p>try—catch—finally </p>
<blockquote>
<p>java提供try和catch块来处理异常，try块用于包含可能出错的代码，catch块用与处理try块中发生的异常，可以根据需要在程序中有多个try- catch块。</p>
<p>可以省略finally</p>
</blockquote>
<p>细节：</p>
<ul>
<li>==如果异常发生了，则异常发生后面的代码不会立即执行，直接进入到catch块中==。</li>
<li>如果异常没有发生，则顺序执行try的代码块，不会进入到catch中。</li>
<li>如果希望不管是否发生异常，都执行某段代码，使用finally块。(当出现异常却抛出时，会优先执行finally再往外继续处理)</li>
<li>可以有多个catch语句，不同的异常(进行不同的业务处理)，要求父类异常在后，子类异常在前，如果发生异常，只会匹配一个catch。</li>
</ul>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">try</span> <span class="token punctuation">&#123;</span>
<span class="token class-name">String</span> str <span class="token operator">=</span> <span class="token string">"数字转换"</span><span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span> 
<span class="token keyword">catch</span> <span class="token punctuation">(</span>子类异常 e<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>            <span class="token comment">//同时可以捕获多种异常</span>
<span class="token comment">//处理异常代码</span>
<span class="token punctuation">&#125;</span>
<span class="token keyword">catch</span> <span class="token punctuation">(</span>父类异常 e<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
<span class="token comment">//处理异常代码</span>
<span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>



<p>可以直接进行try-finally配合使用，这种用法相当于没有捕获异常，因此程序会直接崩掉。但是finally中语句会执行。</p>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">try</span> <span class="token punctuation">&#123;</span>
          可能有异常的代码
        <span class="token punctuation">&#125;</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span><span class="token class-name">Exception</span> e<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
           <span class="token comment">//当系统发生异常时，系统将异常封装成Exception对象 e传递给catchS</span>
  		  <span class="token comment">//得到异常对象后可以自己处理</span>
    	   <span class="token comment">//如果try代码块未产生异常，catch不会执行。</span>
        <span class="token punctuation">&#125;</span>
        <span class="token keyword">finally</span> <span class="token punctuation">&#123;</span>
            <span class="token comment">//不管try代码块是否有异常产生，始终要执行finally。</span>
            <span class="token comment">//通常将释放资源的代码放置</span>
        <span class="token punctuation">&#125;</span>
<span class="token comment">//例子：</span>
<span class="token keyword">try</span> <span class="token punctuation">&#123;</span>
          <span class="token class-name">String</span> str <span class="token operator">=</span> <span class="token string">"数字转换"</span><span class="token punctuation">;</span>
        <span class="token punctuation">&#125;</span> 
<span class="token keyword">catch</span> <span class="token punctuation">(</span><span class="token class-name">NumberFormatException</span> e<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>            <span class="token comment">//同时可以捕获多种异常</span>
           <span class="token keyword">int</span> a <span class="token operator">=</span> <span class="token class-name">Integer</span><span class="token punctuation">.</span><span class="token function">pareInt</span><span class="token punctuation">(</span>str<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">&#125;</span>
<span class="token keyword">catch</span> <span class="token punctuation">(</span><span class="token class-name">Exception</span> e<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
           <span class="token keyword">int</span> a <span class="token operator">=</span> <span class="token class-name">Integer</span><span class="token punctuation">.</span><span class="token function">pareInt</span><span class="token punctuation">(</span>str<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">&#125;</span>
<span class="token keyword">finally</span> <span class="token punctuation">&#123;</span>
            <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"输出"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>throws</p>
<blockquote>
<p>使用throws抛出异常最终被抛出到JVM，JVM的处理方式为;输出异常信息，退出程序。</p>
<p>如果没有显示的处理异常，默认就是throws。(方法会有一个不显示的throws Exception)</p>
</blockquote>
<p>如果一个方法可能生成某种异常，但是并不能确定如何处理这种异常，则此方法应显示的声明抛出异常，表明该方法将不对这些异常进行处理，而由该方法的调用者负责处理。</p>
<p>在方法声明中使用throws可以声明抛出异常的列表，throws后面的异常类型可以时方法中产生的异常类型，也可以是他的父类。</p>
<p>细节：</p>
<ul>
<li>对于编译异常，程序中必须处理，</li>
<li>对于运行时异常，方法中如果没有处理，默认就是throws的处理方式。</li>
<li>子类重写父类的方法时，对抛出异常的规定：子类重写的方法，所抛出的异常类型要么和父类抛出的异常一致，要么为父类抛出的异常的类型的子类型。(==子类不能扩大父类抛出异常的类型==)</li>
<li>在throws中，如果有方法try-catch，就相当于处理异常，就可以不必throws。</li>
<li>方法抛出编译异常时，使用该方法必须处理编译时异常。</li>
<li>方法抛出运行时异常时，使用该方法不需要处理异常，由默认的处理异常机制。</li>
</ul>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token keyword">throws</span> <span class="token class-name">Exception</span> <span class="token punctuation">&#123;</span>  <span class="token comment">//可以是父类也可以是发生的异常，也可以是一个异常的列表，可以抛出多个异常</span>

    <span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span></span></code></pre>



<h4 id="31-5-自定义异常"><a href="#31-5-自定义异常" class="headerlink" title="31.5  自定义异常"></a>31.5  自定义异常</h4><blockquote>
<p>当程序中出现某些‘错误‘，但该错误信息并没有在Throwable子类中描述处理，这个时候可以自己设计异常类用于描述该错误信息。</p>
</blockquote>
<p>自定义异常的步骤</p>
<ol>
<li><p>定义类：自定义异常类名，继承Exception或者RuntimeException</p>
</li>
<li><p>如果继承Exception，属于编译异常。</p>
</li>
<li><p>如果继承RuntimeException，属于运行异常。(一般来说继承RunTimeException)</p>
</li>
<li><p>在自定义异常类中创建构造器，使用super()方法向父类构造器传递异常显示信息。(super方法接收的是一个字符串类型)</p>
</li>
<li><p>在方法体中，使用throw关键字手动调用异常。</p>
</li>
</ol>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">throw</span> <span class="token keyword">new</span> 异常类<span class="token punctuation">(</span>异常输出字符<span class="token punctuation">)</span>；<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>



<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">public</span> <span class="token keyword">class</span> exection <span class="token punctuation">&#123;</span>
    <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span>  <span class="token punctuation">&#123;</span>
            <span class="token keyword">throw</span> <span class="token keyword">new</span> <span class="token class-name">AgeException</span><span class="token punctuation">(</span><span class="token string">"异常"</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//手动调用自定义异常</span>
    <span class="token punctuation">&#125;</span>
<span class="token punctuation">&#125;</span>
<span class="token comment">//自定义异常类，继承RuntimeException或者Exception</span>
<span class="token keyword">class</span> <span class="token class-name">AgeException</span> <span class="token keyword">extends</span> <span class="token class-name">RuntimeException</span><span class="token punctuation">&#123;</span>
    <span class="token keyword">public</span> <span class="token class-name">AgeException</span><span class="token punctuation">(</span><span class="token class-name">String</span> message<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
        <span class="token keyword">super</span><span class="token punctuation">(</span>message<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">&#125;</span>
<span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<table>
<thead>
<tr>
<th></th>
<th>意义</th>
<th>位置</th>
<th>后接</th>
</tr>
</thead>
<tbody><tr>
<td>throws</td>
<td>异常处理的一种方式</td>
<td>方法声明处</td>
<td>异常类型</td>
</tr>
<tr>
<td>throw</td>
<td>手动生成异常的关键字</td>
<td>方法体中</td>
<td>异常对象</td>
</tr>
</tbody></table>
<h3 id="32-包装类"><a href="#32-包装类" class="headerlink" title="32 包装类"></a>32 包装类</h3><blockquote>
<p>针对八种基本数据类型的引用类型–包装类(wrapper)</p>
<p>有类的特点，就可以调用类中的方法。</p>
</blockquote>
<table>
<thead>
<tr>
<th>基本数据类型</th>
<th>包装类</th>
</tr>
</thead>
<tbody><tr>
<td>boolean</td>
<td><strong>Boolean</strong></td>
</tr>
<tr>
<td>char</td>
<td><strong>Character</strong></td>
</tr>
<tr>
<td>byte</td>
<td>Byte</td>
</tr>
<tr>
<td>short</td>
<td>Short</td>
</tr>
<tr>
<td>int</td>
<td>Integer</td>
</tr>
<tr>
<td>long</td>
<td>Long</td>
</tr>
<tr>
<td>float</td>
<td>Float</td>
</tr>
<tr>
<td>double</td>
<td>Double</td>
</tr>
</tbody></table>
<blockquote>
<p>Byte,Integer,Long,Float,Double,Short 都继承Number</p>
<p>Boolean，Character继承objhect</p>
</blockquote>
<h4 id="32-1-包装类和基本数据的转换"><a href="#32-1-包装类和基本数据的转换" class="headerlink" title="32.1 包装类和基本数据的转换"></a>32.1 包装类和基本数据的转换</h4><blockquote>
<p>包装类和基本数据类型可以互相转换。</p>
<p>jdk5 前的手动装箱和拆箱方式，装箱：基本类型 -&gt; 包装类型</p>
</blockquote>
<p>拆箱和装箱有两种方式。</p>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">10</span><span class="token punctuation">;</span>    
<span class="token comment">//手动装箱</span>
<span class="token class-name">Integer</span> integer <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Integer</span><span class="token punctuation">(</span>i<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token class-name">Integer</span> integer <span class="token operator">=</span> <span class="token class-name">Integer</span><span class="token punctuation">.</span><span class="token function">valueOf</span><span class="token punctuation">(</span>i<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">//手动拆箱</span>
<span class="token keyword">int</span> i<span class="token operator">=</span> integer<span class="token punctuation">.</span><span class="token function">intValue</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>jdk5 以后(含jdk5)的自动拆箱和装箱方式。</p>
<p>自动装箱使用的ValueOf()</p>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">10</span><span class="token punctuation">;</span>
<span class="token comment">//自动装箱</span>
<span class="token class-name">Integer</span> integer <span class="token operator">=</span> i；  <span class="token comment">//底层实际上使用Integer.valueOf(i);</span>
<span class="token comment">//自动拆箱</span>
<span class="token keyword">int</span> i <span class="token operator">=</span> integer；
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>自动装箱底层调用的是valueOf方法。</p>
<p>Integer.valueOf()中接收的值如果在-128到127范围内，不会new Integer，直接使用缓存存储数据；在创建时就会创建一个IntegerCache.cache数组，数组范围在-128到127，只要在此范围内不需要重新new。</p>
<p>含有缓冲数组的包装类：Integer，Char，Short</p>
<p>==包装类和基本类作比较，只要存在基本数据类型，“ == ”号比较的就是值是否相等。==</p>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token class-name">Integer</span> <span class="token function">valueOf</span><span class="token punctuation">(</span><span class="token keyword">int</span> i<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
<span class="token comment">//IntegerCache.low 为-128 IntegerCache.high为 127</span>
  <span class="token keyword">if</span> <span class="token punctuation">(</span>i <span class="token operator">>=</span> <span class="token class-name">IntegerCache</span><span class="token punctuation">.</span>low <span class="token operator">&amp;&amp;</span> i <span class="token operator">&lt;=</span> <span class="token class-name">IntegerCache</span><span class="token punctuation">.</span>high<span class="token punctuation">)</span>
      <span class="token keyword">return</span> <span class="token class-name">IntegerCache</span><span class="token punctuation">.</span>cache<span class="token punctuation">[</span>i <span class="token operator">+</span> <span class="token punctuation">(</span><span class="token operator">-</span><span class="token class-name">IntegerCache</span><span class="token punctuation">.</span>low<span class="token punctuation">)</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
  <span class="token keyword">return</span> <span class="token keyword">new</span> <span class="token class-name">Integer</span><span class="token punctuation">(</span>i<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>



<p>三元运算符补充：</p>
<blockquote>
<p>三元运算符为一个整体，会提升精度</p>
</blockquote>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token class-name">Object</span> obj <span class="token operator">=</span> <span class="token boolean">true</span><span class="token operator">?</span><span class="token keyword">new</span> <span class="token class-name">Integer</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token operator">:</span><span class="token keyword">new</span> <span class="token class-name">Double</span><span class="token punctuation">(</span><span class="token number">2.0</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>obj<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//输出为1.0</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre>

<h4 id="32-2-包装类和String数据类型的相互转换。"><a href="#32-2-包装类和String数据类型的相互转换。" class="headerlink" title="32.2 包装类和String数据类型的相互转换。"></a>32.2 包装类和String数据类型的相互转换。</h4><blockquote>
<p>包装类和String类型可以相互转换。</p>
</blockquote>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token comment">//包装类 ----> String</span>
<span class="token class-name">Integer</span> i <span class="token operator">=</span> <span class="token number">10</span>；
<span class="token comment">//方法一</span>
<span class="token class-name">String</span> str <span class="token operator">=</span> i<span class="token punctuation">.</span><span class="token function">toString</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">//方式二</span>
<span class="token class-name">String</span> str1 <span class="token operator">=</span> <span class="token class-name">String</span><span class="token punctuation">.</span><span class="token function">valueOf</span><span class="token punctuation">(</span>i<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">//方式三</span>
<span class="token class-name">String</span> str2 <span class="token operator">=</span> i <span class="token operator">+</span> <span class="token string">""</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>



<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token comment">//String ----> 包装类</span>
<span class="token class-name">String</span> str <span class="token operator">=</span> <span class="token string">"1"</span><span class="token punctuation">;</span>
<span class="token comment">//方式一</span>
<span class="token class-name">Integer</span> j <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Integer</span><span class="token punctuation">(</span>s1<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">//方式二</span>
<span class="token class-name">Integer</span> j <span class="token operator">=</span> <span class="token class-name">Integer</span><span class="token punctuation">.</span><span class="token function">valueOf</span><span class="token punctuation">(</span>s2<span class="token punctuation">)</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>



<h3 id="33-String类"><a href="#33-String类" class="headerlink" title="33. String类"></a>33. String类</h3><ol>
<li>String 对象用于保存字符串，也就是一组字符序列。</li>
<li>字符串常量对象用双括号括起来的字符序列。</li>
</ol>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token class-name">String</span> str <span class="token operator">=</span> <span class="token string">"doubao"</span><span class="token punctuation">;</span>字符串的字符使用<span class="token class-name">Unicode</span>字符编码，一个字符<span class="token punctuation">(</span>不区分字母和汉字<span class="token punctuation">)</span>占用两个字节。（<span class="token keyword">char</span>占用两个字节）<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>

<ol start="3">
<li>String类是被final修饰的，不允许被继承。</li>
<li>String类有许多构造方法，常用的构造方法：</li>
</ol>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token class-name">String</span> str <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">String</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token class-name">String</span> str <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">String</span><span class="token punctuation">(</span><span class="token class-name">String</span> original<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token class-name">String</span> str <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">String</span><span class="token punctuation">(</span><span class="token keyword">char</span><span class="token punctuation">[</span><span class="token punctuation">]</span> a<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token class-name">String</span> str <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">String</span><span class="token punctuation">(</span><span class="token keyword">char</span><span class="token punctuation">[</span><span class="token punctuation">]</span> a<span class="token punctuation">,</span><span class="token keyword">int</span> statIndex<span class="token punctuation">,</span><span class="token keyword">int</span> count<span class="token punctuation">)</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span></span></code></pre>

<ol start="5">
<li><p>String类实现了接口 Serializable说明String可以串行化。(可以在网络传输)</p>
</li>
<li><p>String类实现了接口Comparable说明String类可以比较。</p>
</li>
<li><p>String类中有属性private final char value[]用于存放字符串内容。value是final类型的，不能修改。(value[]的地址不能修改)但是单个字符的内容可以修改。</p>
</li>
</ol>
<h4 id="33-1-String创建"><a href="#33-1-String创建" class="headerlink" title="33.1 String创建"></a>33.1 String创建</h4><blockquote>
<p>String 创建对象的两种方式。</p>
</blockquote>
<p>方式一： 直接赋值 String str = “douabo”;</p>
<ul>
<li>先从常量池查看是否有“doubao”的数据空间，如果有，直接指向，如果没有则重新创建，然后指向，str最终指向的是常量池的空间地址。</li>
</ul>
<p>方式二：调用构造器 String s = new String(“doubao”)；</p>
<ul>
<li>现在堆中创建空间，里面维护了value属性，指向常量池的“doubao”。如果常量池中没有“douabo”，重新创建，如果有，直接通过value指向，最终指向的是堆中的空间地址。</li>
</ul>
<h4 id="33-2-String类的特性："><a href="#33-2-String类的特性：" class="headerlink" title="33.2 String类的特性："></a>33.2 String类的特性：</h4><blockquote>
<p>编译器会进行优化，不会浪费资源。</p>
</blockquote>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token class-name">String</span> str <span class="token operator">=</span> <span class="token string">"dou"</span> <span class="token operator">+</span> <span class="token string">"bao"</span><span class="token punctuation">;</span><span class="token comment">//两个常量相加看池中，</span>
<span class="token class-name">String</span> str1 <span class="token operator">=</span> str2 <span class="token operator">+</span> str3<span class="token punctuation">;</span> <span class="token comment">//两个变量相加看堆</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre>





<h4 id="33-3-String类的常用方法"><a href="#33-3-String类的常用方法" class="headerlink" title="33.3 String类的常用方法"></a>33.3 String类的常用方法</h4><blockquote>
<p>String 类是保存字符串常量的，每次更新都需要开辟空间，效率较低，因此java设计者提供了StringBuilder和StringBuffer来增强String的功能，并提高效率。</p>
</blockquote>
<table>
<thead>
<tr>
<th>方法</th>
<th>作用</th>
<th>备注</th>
</tr>
</thead>
<tbody><tr>
<td>equals</td>
<td>判断内容是否相等</td>
<td></td>
</tr>
<tr>
<td>equalslgnoreCase</td>
<td>忽略大小写判断内容是否相等</td>
<td></td>
</tr>
<tr>
<td>length</td>
<td>获得字符的个数，字符串的长度</td>
<td></td>
</tr>
<tr>
<td>indexOf</td>
<td>获取字符在字符串中第一次出现的索引，索引从0开始</td>
<td>未找到返回-1</td>
</tr>
<tr>
<td>lastIndexOf</td>
<td>获取字符在字符串中最后一次出现的索引，索引从0开始</td>
<td>未找到返回-1</td>
</tr>
<tr>
<td>substring</td>
<td>截取指定范围的字串</td>
<td>包括开始，不包括结束</td>
</tr>
<tr>
<td>trim</td>
<td>去前后空格</td>
<td></td>
</tr>
<tr>
<td>charAt</td>
<td>获取某索引处的字符</td>
<td>不能使用Str[index]</td>
</tr>
<tr>
<td>toUpperCase</td>
<td>将字符串转换为大写</td>
<td></td>
</tr>
<tr>
<td>toLowerCase</td>
<td>将字符串转换为小写</td>
<td></td>
</tr>
<tr>
<td>concat</td>
<td>拼接字符串</td>
<td></td>
</tr>
<tr>
<td>compareTo</td>
<td>比较两个字符串的大小</td>
<td></td>
</tr>
<tr>
<td>toCharArray</td>
<td>转换成字符数组</td>
<td></td>
</tr>
<tr>
<td>format</td>
<td>使用占位符格式化字符串</td>
<td>有点像c语言</td>
</tr>
<tr>
<td>replace</td>
<td>替换字符串中的字符</td>
<td>对原本字符串无影响，返回替换后的字符串</td>
</tr>
<tr>
<td>split</td>
<td>分割字符串</td>
<td>传入分割依据为分隔参数</td>
</tr>
</tbody></table>
<hr>
<h3 id="34-StringBuffer"><a href="#34-StringBuffer" class="headerlink" title="34. StringBuffer"></a>34. StringBuffer</h3><ul>
<li>java .long.StringBuffer代表可变字符序列。可以对字符串的内容进行增删。</li>
<li>==StringBuffer是线程安全的==。</li>
<li>很多方法与String相同，但==是StringBuffer是可变长度的。==</li>
<li>StringBuffer是一个容器。</li>
<li>StringBuffer实现了Serializable，即StringBuffer的对象是可以串行化的。</li>
<li>StringBuffer继承了父类AbstractStringBuilder，在父类中，有属性char[] value，不是final。</li>
<li>该value数组存放字符串内容，存放在堆中。(String存放在常量池)</li>
<li>StringBuffer是一个final类。不能被继承。</li>
<li><code>System.out.println(strBuffer)</code> 实际上就是调用StringBuilder的toString()方法。</li>
</ul>
<h4 id="String类与StringBuffer类区别"><a href="#String类与StringBuffer类区别" class="headerlink" title="String类与StringBuffer类区别"></a>String类与StringBuffer类区别</h4><p>String 保存的是字符串常量，里面的值不能修改，每次String类的更新实际上就是更改地址，效率较低。<code>private final char value[];</code></p>
<p>StringBuffer 存的是字符串变量，里面的值可以更改，每次StringBuffer 更新实际上可以更新内容，不用每次更新地址，效率比较高。<code>char[] value;</code>（空间大小不够时也会更新地址）</p>
<h4 id="StringBuffer构造器"><a href="#StringBuffer构造器" class="headerlink" title="StringBuffer构造器"></a>StringBuffer构造器</h4><blockquote>
<p>StringBuffer()构造器</p>
</blockquote>
<ol>
<li>StringBuffer() 构造的字符串缓冲区，其初始容量为16个字符。</li>
<li>StringBuffer(CharSequence seq) 构造一个字符串缓冲区，他包含指定的CharSequence相同的字符。</li>
<li>StringBuffer(int 容量） 构造一个不带字符，但是具有指定初始容量的字符串缓冲区，即对char[]大小进行指定。</li>
<li>StringBuffer(String str) 构造一个字符串缓冲区。将其内容初始化为指定的字符串内容。    //其中value数组的长度为str.length + 16</li>
</ol>
<h4 id="String-与StringBuffer互相进行转换"><a href="#String-与StringBuffer互相进行转换" class="headerlink" title="String 与StringBuffer互相进行转换"></a>String 与StringBuffer互相进行转换</h4><blockquote>
<p>String转换为stringBuffer</p>
</blockquote>
<ul>
<li>==调用new StringBuffer(str)构造器后如果传入字符为null则会报错。抛出空指针异常==。</li>
</ul>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token comment">//String----> StringBuffer</span>
<span class="token class-name">String</span> str <span class="token operator">=</span> <span class="token string">"douabo"</span><span class="token punctuation">;</span>
<span class="token comment">//方式一</span>
<span class="token class-name">StringBuffer</span> strb <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">StringBuffer</span><span class="token punctuation">(</span>str<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//返回的才是StringBuffer类型的对象与原来字符串无影响</span>
<span class="token comment">//方式二</span>
<span class="token class-name">StringBuffer</span> strb2 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">StringBuffer</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
strb2<span class="token punctuation">.</span><span class="token function">append</span><span class="token punctuation">(</span>str<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">//返回的才是StringBuffer类型的对象与原来字符串无影响</span>

<span class="token comment">//StringBuffer---->String</span>
<span class="token class-name">StringBuffer</span> strb <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">StringBuffer</span><span class="token punctuation">(</span><span class="token string">"douabo"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">//方式一</span>
<span class="token class-name">String</span> str <span class="token operator">=</span> strb<span class="token punctuation">.</span><span class="token function">toString</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">//方式二</span>
<span class="token class-name">String</span> str2 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">String</span><span class="token punctuation">(</span>strb<span class="token punctuation">)</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>





<h4 id="StringBuffer常用方法"><a href="#StringBuffer常用方法" class="headerlink" title="StringBuffer常用方法"></a>StringBuffer常用方法</h4><p>增加： append()  </p>
<p>删除：delete(start,end)</p>
<p>改：replace(start,end,string)</p>
<p>查找 ： indexOf(str) </p>
<p>插入： insert()</p>
<p>获取长度: length</p>
<h5 id="append-增加"><a href="#append-增加" class="headerlink" title="append() 增加"></a>append() 增加</h5><blockquote>
<p>使用append()方法向指定的StringBuffer对象最后添加指定字符串。</p>
</blockquote>
<p>当append(str)==添加的字符串为null时，不会报错==，而是返回一个字符串长度为四的”null”。（调用构造方法时输入null会报错）</p>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token class-name">StringBuffer</span> strb <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">StringBuffer</span><span class="token punctuation">(</span><span class="token string">"douabo"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>strb<span class="token punctuation">)</span><span class="token punctuation">;</span>
strb<span class="token punctuation">.</span><span class="token function">append</span><span class="token punctuation">(</span><span class="token string">"xin"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>strb<span class="token punctuation">)</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span></span></code></pre>



<h5 id="delete-删除"><a href="#delete-删除" class="headerlink" title="delete() 删除"></a>delete() 删除</h5><blockquote>
<p>使用delete(start,end)删除指定开始到指定末尾之间的元素。==包括开始元素不包括结束元素。==</p>
</blockquote>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token class-name">StringBuffer</span> strb <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">StringBuffer</span><span class="token punctuation">(</span><span class="token string">"douabo"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
strb<span class="token punctuation">.</span><span class="token function">delete</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span><span class="token number">3</span><span class="token punctuation">)</span><span class="token punctuation">;</span>  <span class="token comment">//包含开始元素不包含结束元素</span>
<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>strb<span class="token punctuation">)</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span></span></code></pre>





<h5 id="replace-替换"><a href="#replace-替换" class="headerlink" title="replace() 替换"></a>replace() 替换</h5><blockquote>
<p>使用replace(start,end,replace) 将指定开始和指定结束之间的字符使用指定的字符进行替换。==包含开始元素不包含结束元素。==</p>
</blockquote>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token class-name">StringBuffer</span> strb <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">StringBuffer</span><span class="token punctuation">(</span><span class="token string">"douabo"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
strb<span class="token punctuation">.</span><span class="token function">replace</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span><span class="token number">3</span><span class="token punctuation">,</span><span class="token string">"豆包"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>strb<span class="token punctuation">)</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span></span></code></pre>



<h5 id="indexOf-str"><a href="#indexOf-str" class="headerlink" title="indexOf(str)"></a>indexOf(str)</h5><blockquote>
<p>查找字符串中指定字符串第一次出现的索引。如果没有查找到返回-1.</p>
</blockquote>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token class-name">StringBuffer</span> strb <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">StringBuffer</span><span class="token punctuation">(</span><span class="token string">"douabo"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>strb<span class="token punctuation">.</span><span class="token function">indexOf</span><span class="token punctuation">(</span><span class="token string">"a"</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//3</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span></span></code></pre>



<h5 id="insert-start-string-插入"><a href="#insert-start-string-插入" class="headerlink" title="insert(start,string) 插入"></a>insert(start,string) 插入</h5><blockquote>
<p>在指定索引处插入指定字符串。原本指定索引位置的字符串后移。</p>
</blockquote>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token class-name">StringBuffer</span> strb <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">StringBuffer</span><span class="token punctuation">(</span><span class="token string">"douabo"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
strb<span class="token punctuation">.</span><span class="token function">insert</span><span class="token punctuation">(</span><span class="token number">3</span><span class="token punctuation">,</span><span class="token string">"豆包"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>strb<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//dou豆包abo</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span></span></code></pre>



<h3 id="35-StringBuilder"><a href="#35-StringBuilder" class="headerlink" title="35. StringBuilder"></a>35. StringBuilder</h3><blockquote>
<p>一个可变字符序列，为此类提供一个与StringBuffer兼容的API，但不保证同步，该类被设计用作StringBuffer的一个建议替换，用在字符串缓冲区被==单个线程使用的时候==，如果可能，建议采用该类，因为在大多数实现中，StringBuilder要比StringBuffer要快。</p>
</blockquote>
<p>但是StringBuilder不是线程安全。</p>
<p>StringBuilder是一个final类，是不可以被继承的。</p>
<p>在StringBuilder上的主要操作是append和insert方法，可重载这些方法，以接收任意类型的数据。</p>
<ul>
<li>StringBuilder实现了Serializable，即StringBuilder的对象是可以串行化的。(对象可以网络传输，可以保存到文件)</li>
<li>StringBuilder继承了父类AbstractStringBuilder，在父类中，有属性char[] value，不是final。因此字符序列是存储在堆中的。</li>
<li>StringBuilder的方法，没有做互斥的处理，即没有synchronized关键字，因此在单线程的情况下使用。</li>
</ul>
<h4 id="35-1-StringBuilder常用方法"><a href="#35-1-StringBuilder常用方法" class="headerlink" title="35.1 StringBuilder常用方法"></a>35.1 StringBuilder常用方法</h4><blockquote>
<p>StringBuilder和StringBuffer均代表可变的字符序列，方法是一样的，所以使用和StringBuffer一样。(StringBuilder和StringBuffer都继承了AbstractStringBuilder父类)</p>
</blockquote>
<p>增加： append()  </p>
<p>删除：delete(start,end)</p>
<p>改：replace(start,end,string)</p>
<p>查找 ： indexOf(str) </p>
<p>插入： insert()</p>
<p>获取长度: length</p>
<h5 id="append-增加-1"><a href="#append-增加-1" class="headerlink" title="append() 增加"></a>append() 增加</h5><blockquote>
<p>使用append()方法向指定的StringBuffer对象最后添加指定字符串。</p>
</blockquote>
<p>当append(str)添加的字符串为null时，不会报错，而是返回一个字符串长度为四的”null”。</p>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token class-name">StringBuffer</span> strb <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">StringBuffer</span><span class="token punctuation">(</span><span class="token string">"douabo"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>strb<span class="token punctuation">)</span><span class="token punctuation">;</span>
strb<span class="token punctuation">.</span><span class="token function">append</span><span class="token punctuation">(</span><span class="token string">"xin"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>strb<span class="token punctuation">)</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span></span></code></pre>



<h5 id="delete-删除-1"><a href="#delete-删除-1" class="headerlink" title="delete() 删除"></a>delete() 删除</h5><blockquote>
<p>使用delete(start,end)删除指定开始到指定末尾之间的元素。==包括开始元素不包括结束元素。==</p>
</blockquote>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token class-name">StringBuilder</span> strb <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">StringBuilder</span><span class="token punctuation">(</span><span class="token string">"douabo"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
strb<span class="token punctuation">.</span><span class="token function">delete</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span><span class="token number">3</span><span class="token punctuation">)</span><span class="token punctuation">;</span>  <span class="token comment">//包含开始元素不包含结束元素</span>
<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>strb<span class="token punctuation">)</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span></span></code></pre>





<h5 id="replace-替换-1"><a href="#replace-替换-1" class="headerlink" title="replace() 替换"></a>replace() 替换</h5><blockquote>
<p>使用replace(start,end,replace) 将指定开始和指定结束之间的字符使用指定的字符进行替换。==包含开始元素不包含结束元素。==</p>
</blockquote>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token class-name">StringBuilder</span> strb <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">StringBuilder</span><span class="token punctuation">(</span><span class="token string">"douabo"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
strb<span class="token punctuation">.</span><span class="token function">replace</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span><span class="token number">3</span><span class="token punctuation">,</span><span class="token string">"豆包"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>strb<span class="token punctuation">)</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span></span></code></pre>



<h5 id="indexOf-str-1"><a href="#indexOf-str-1" class="headerlink" title="indexOf(str)"></a>indexOf(str)</h5><blockquote>
<p>查找字符串中指定字符串第一次出现的索引。如果没有查找到返回-1.</p>
</blockquote>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token class-name">StringBuilder</span> strb <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">StringBuilder</span><span class="token punctuation">(</span><span class="token string">"douabo"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>strb<span class="token punctuation">.</span><span class="token function">indexOf</span><span class="token punctuation">(</span><span class="token string">"a"</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//3</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span></span></code></pre>



<h5 id="insert-start-string-插入-1"><a href="#insert-start-string-插入-1" class="headerlink" title="insert(start,string) 插入"></a>insert(start,string) 插入</h5><blockquote>
<p>在指定索引处插入指定字符串。原本指定索引位置的字符串后移。</p>
</blockquote>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token class-name">StringBuilder</span> strb <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">StringBuilder</span><span class="token punctuation">(</span><span class="token string">"douabo"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
strb<span class="token punctuation">.</span><span class="token function">insert</span><span class="token punctuation">(</span><span class="token number">3</span><span class="token punctuation">,</span><span class="token string">"豆包"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>strb<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//dou豆包abo</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span></span></code></pre>







<h4 id="35-2-String-与-StringBuffer-与-StringBuilder的比较"><a href="#35-2-String-与-StringBuffer-与-StringBuilder的比较" class="headerlink" title="35.2 String 与 StringBuffer 与 StringBuilder的比较"></a>35.2 String 与 StringBuffer 与 StringBuilder的比较</h4><blockquote>
<p>StringBuilder 和 StringBuffer非常类似，均代表可变的字符序列，而且方法也一样。</p>
</blockquote>
<p>String: 不可变字符序列，效率低，但是复用率高。</p>
<p>StringBuffer: 可变字符序列，效率较高(增删)，线程安全。</p>
<p>StringBuffer中的方法前加修饰符 synchronized（同步）</p>
<p>StringBuilder：可变字符序列，效率最高，线程不安全。</p>
<h4 id="35-3-String-与-StringBuffer-与-StringBuilder的选择"><a href="#35-3-String-与-StringBuffer-与-StringBuilder的选择" class="headerlink" title="35.3 String 与 StringBuffer 与 StringBuilder的选择"></a>35.3 String 与 StringBuffer 与 StringBuilder的选择</h4><p>如果字符串存在大量的修改操作，一般使用StringBuffer 与 StringBuilder。</p>
<p>如果字符串存在大量的修改操作，并在单线程的情况下，使用StringBuilder</p>
<p>如果字符串存在大量的修改操作，并且在多线程的情况下，使用StringBuffer</p>
<p>如果字符串很少修改，并且被多个对象引用，比如配置信息,使用String</p>
<h3 id="36-Math"><a href="#36-Math" class="headerlink" title="36. Math"></a>36. Math</h3><blockquote>
<p>Math类包含用于执行基本数学运算的方法，如初等指数，对数，平方根和三角函数。</p>
</blockquote>
<p>常用方法：</p>
<blockquote>
<p>Math中的类大多数都是静态方法</p>
</blockquote>
<p>abs() 绝对值</p>
<p>pow() 求幂</p>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token function">pow</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">,</span><span class="token number">4</span><span class="token punctuation">)</span><span class="token punctuation">;</span> 求<span class="token number">2</span>的<span class="token number">4</span>次方<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>

<p>ceil() 向上取整</p>
<p>floor() 向下取整</p>
<p>round() 四舍五入</p>
<p>sqrt() 求开方</p>
<p>random() 求随机数,返回的是0–1之间的随机小数(能取到0不饿能取到1)</p>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token comment">//求a--b之间的整数倍</span>
<span class="token punctuation">(</span><span class="token keyword">int</span><span class="token punctuation">)</span><span class="token punctuation">(</span>a <span class="token operator">+</span> <span class="token class-name">Math</span><span class="token punctuation">.</span><span class="token function">random</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">*</span> <span class="token punctuation">(</span>b<span class="token operator">-</span>a <span class="token operator">+</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre>



<p>max() 求两个数的最大值</p>
<p>min() 求两个数的最小值</p>
<h3 id="37-Arrays"><a href="#37-Arrays" class="headerlink" title="37. Arrays"></a>37. Arrays</h3><blockquote>
<p>Arrays里面包含了一系列静态方法，用于管理或者操作数组。(比如排序和搜索)</p>
</blockquote>
<h4 id="37-1-Arrays常用方法"><a href="#37-1-Arrays常用方法" class="headerlink" title="37.1 Arrays常用方法"></a>37.1 Arrays常用方法</h4><h5 id="toString-NaN"><a href="#toString-NaN" class="headerlink" title="toString()"></a>toString()</h5><blockquote>
<p>Arrays.toString(arr)</p>
<p>返回数组的字符串形式</p>
</blockquote>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token class-name">Integer</span><span class="token punctuation">[</span><span class="token punctuation">]</span> integers <span class="token operator">=</span> <span class="token punctuation">&#123;</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">,</span><span class="token number">3</span><span class="token punctuation">,</span><span class="token number">4</span><span class="token punctuation">,</span><span class="token number">5</span><span class="token punctuation">,</span><span class="token number">7</span><span class="token punctuation">,</span><span class="token number">6</span><span class="token punctuation">&#125;</span><span class="token punctuation">;</span>

<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token class-name">Arrays</span><span class="token punctuation">.</span><span class="token function">toString</span><span class="token punctuation">(</span>integers<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span></span></code></pre>





<h5 id="sort排序-自然排序和定制排序"><a href="#sort排序-自然排序和定制排序" class="headerlink" title="sort排序(自然排序和定制排序)"></a>sort排序(自然排序和定制排序)</h5><blockquote>
<p>Arrays.sort(arr)可以对传入的数组进行排序。</p>
<p>数组为引用类型，所以通过sort排序后，会直接影响到实参。</p>
</blockquote>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token class-name">Integer</span><span class="token punctuation">[</span><span class="token punctuation">]</span> integers <span class="token operator">=</span> <span class="token punctuation">&#123;</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">,</span><span class="token number">3</span><span class="token punctuation">,</span><span class="token number">4</span><span class="token punctuation">,</span><span class="token number">5</span><span class="token punctuation">,</span><span class="token number">7</span><span class="token punctuation">,</span><span class="token number">6</span><span class="token punctuation">&#125;</span><span class="token punctuation">;</span>
<span class="token class-name">Arrays</span><span class="token punctuation">.</span><span class="token function">sort</span><span class="token punctuation">(</span>integers<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token class-name">Arrays</span><span class="token punctuation">.</span><span class="token function">toString</span><span class="token punctuation">(</span>integers<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span></span></code></pre>

<p>sort是重载的，可以通过传入一个接口Comparator实现定制排序。</p>
<p>调用定制排序时，传入两个参数</p>
<ol>
<li>需要排序的数组</li>
<li>实现了Comparator接口的匿名内部类，要求实现compare方法</li>
<li>实现接口中compare()方法的返回值大于或者小于0会影响排序的内部逻辑。本质就是冒泡排序中控制顺序的语句由compare方法返回值控制。</li>
</ol>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token class-name">Integer</span><span class="token punctuation">[</span><span class="token punctuation">]</span> integers <span class="token operator">=</span> <span class="token punctuation">&#123;</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">,</span><span class="token number">3</span><span class="token punctuation">,</span><span class="token number">4</span><span class="token punctuation">,</span><span class="token number">5</span><span class="token punctuation">,</span><span class="token number">7</span><span class="token punctuation">,</span><span class="token number">6</span><span class="token punctuation">&#125;</span><span class="token punctuation">;</span>
  <span class="token class-name">Arrays</span><span class="token punctuation">.</span><span class="token function">sort</span><span class="token punctuation">(</span>integers<span class="token punctuation">,</span> <span class="token keyword">new</span> <span class="token class-name">Comparator</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
      <span class="token annotation punctuation">@Override</span>
      <span class="token keyword">public</span> <span class="token keyword">int</span> <span class="token function">compare</span><span class="token punctuation">(</span><span class="token class-name">Object</span> o1<span class="token punctuation">,</span> <span class="token class-name">Object</span> o2<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
          <span class="token class-name">Integer</span> i1 <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token class-name">Integer</span><span class="token punctuation">)</span>o1<span class="token punctuation">;</span>
          <span class="token class-name">Integer</span> i2 <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token class-name">Integer</span><span class="token punctuation">)</span>o2<span class="token punctuation">;</span>
          <span class="token keyword">return</span> i2<span class="token operator">-</span>i1<span class="token punctuation">;</span>
      <span class="token punctuation">&#125;</span>
  <span class="token punctuation">&#125;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token class-name">Arrays</span><span class="token punctuation">.</span><span class="token function">toString</span><span class="token punctuation">(</span>integers<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>





<h5 id="binarySeach-二分法查找"><a href="#binarySeach-二分法查找" class="headerlink" title="binarySeach 二分法查找"></a>binarySeach 二分法查找</h5><blockquote>
<p>通过二分搜索法进行查找，要求必须已经有顺序。</p>
<p>Arrays.binarySearch(数组,元素);</p>
<p>如果数组中不存在==返回-(low + 1)==。(应该存在的位置+1取负数)</p>
</blockquote>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
  <span class="token class-name">Integer</span><span class="token punctuation">[</span><span class="token punctuation">]</span> integers <span class="token operator">=</span> <span class="token punctuation">&#123;</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">,</span><span class="token number">3</span><span class="token punctuation">,</span><span class="token number">4</span><span class="token punctuation">,</span><span class="token number">5</span><span class="token punctuation">,</span><span class="token number">7</span><span class="token punctuation">,</span><span class="token number">6</span><span class="token punctuation">&#125;</span><span class="token punctuation">;</span>
  <span class="token class-name">Arrays</span><span class="token punctuation">.</span><span class="token function">sort</span><span class="token punctuation">(</span>integers<span class="token punctuation">)</span><span class="token punctuation">;</span>
  
  <span class="token class-name">Arrays</span><span class="token punctuation">.</span><span class="token function">binarySearch</span><span class="token punctuation">(</span>integers<span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
         			               			          <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token class-name">Arrays</span><span class="token punctuation">.</span><span class="token function">binarySearch</span><span class="token punctuation">(</span>integers<span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>





<h5 id="copyOf-数组复制"><a href="#copyOf-数组复制" class="headerlink" title="copyOf 数组复制"></a>copyOf 数组复制</h5><blockquote>
<p>Arrays.copyOf(数组,元素个数);</p>
<p>从指定数组中复制指定个数个元素组成新的数组返回.</p>
<p>如果需要拷贝的个数大于原本的个数，则在多余的部分赋值为null。</p>
<p>如果需要拷贝的个数为负数，则抛出异常。</p>
</blockquote>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token class-name">Integer</span><span class="token punctuation">[</span><span class="token punctuation">]</span> arr <span class="token operator">=</span> <span class="token punctuation">&#123;</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">,</span><span class="token number">3</span><span class="token punctuation">,</span><span class="token number">4</span><span class="token punctuation">,</span><span class="token number">5</span><span class="token punctuation">,</span><span class="token number">7</span><span class="token punctuation">,</span><span class="token number">6</span><span class="token punctuation">&#125;</span><span class="token punctuation">;</span>

<span class="token class-name">Integer</span><span class="token punctuation">[</span><span class="token punctuation">]</span> newarr <span class="token operator">=</span> <span class="token class-name">Arrays</span><span class="token punctuation">.</span><span class="token function">copyOf</span><span class="token punctuation">(</span>arr<span class="token punctuation">,</span>arr<span class="token punctuation">.</span>length<span class="token punctuation">)</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span></span></code></pre>





<h5 id="fill-数组填充"><a href="#fill-数组填充" class="headerlink" title="fill 数组填充"></a>fill 数组填充</h5><blockquote>
<p>Arrays.fill(数组,元素);</p>
<p>向指定数组中填充数组，可以理解成替换原本的元素。</p>
</blockquote>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token class-name">Integer</span><span class="token punctuation">[</span><span class="token punctuation">]</span> integers <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Integer</span><span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">&#123;</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">,</span><span class="token number">3</span><span class="token punctuation">,</span><span class="token number">4</span><span class="token punctuation">,</span><span class="token number">5</span><span class="token punctuation">,</span><span class="token number">7</span><span class="token punctuation">,</span><span class="token number">6</span><span class="token punctuation">&#125;</span><span class="token punctuation">;</span>

  <span class="token class-name">Arrays</span><span class="token punctuation">.</span><span class="token function">fill</span><span class="token punctuation">(</span>integers<span class="token punctuation">,</span><span class="token number">55</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

  <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token class-name">Arrays</span><span class="token punctuation">.</span><span class="token function">toString</span><span class="token punctuation">(</span>integers<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span></span></code></pre>





<h5 id="equals-比较"><a href="#equals-比较" class="headerlink" title="equals 比较"></a>equals 比较</h5><blockquote>
<p>比较两个数组元素内容是否完全一致</p>
<p>Arrays.equals(数组,数组)</p>
<p>如果一致返回true如果不一致返回false。</p>
</blockquote>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token class-name">Integer</span><span class="token punctuation">[</span><span class="token punctuation">]</span> integers <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Integer</span><span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">&#123;</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">,</span><span class="token number">3</span><span class="token punctuation">,</span><span class="token number">4</span><span class="token punctuation">,</span><span class="token number">5</span><span class="token punctuation">,</span><span class="token number">7</span><span class="token punctuation">,</span><span class="token number">6</span><span class="token punctuation">&#125;</span><span class="token punctuation">;</span>
  <span class="token class-name">Integer</span><span class="token punctuation">[</span><span class="token punctuation">]</span> integers1 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Integer</span><span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">&#123;</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">,</span><span class="token number">3</span><span class="token punctuation">,</span><span class="token number">4</span><span class="token punctuation">,</span><span class="token number">5</span><span class="token punctuation">,</span><span class="token number">7</span><span class="token punctuation">,</span><span class="token number">6</span><span class="token punctuation">&#125;</span><span class="token punctuation">;</span>

  <span class="token keyword">boolean</span> istrue <span class="token operator">=</span> <span class="token class-name">Arrays</span><span class="token punctuation">.</span><span class="token function">equals</span><span class="token punctuation">(</span>integers<span class="token punctuation">,</span>integers1<span class="token punctuation">)</span><span class="token punctuation">;</span>

  <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>istrue<span class="token punctuation">)</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>





<h5 id="asList-转换列表"><a href="#asList-转换列表" class="headerlink" title="asList 转换列表"></a>asList 转换列表</h5><blockquote>
<p>将一组值转换为list</p>
<p>Arrays.asList(1,2,3,4,5)</p>
<p>asList的运行类型为 java.util.Arrays#ArrayList,是内部类。</p>
</blockquote>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token class-name">List</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">Integer</span><span class="token punctuation">></span></span> asList <span class="token operator">=</span> <span class="token class-name">Arrays</span><span class="token punctuation">.</span><span class="token function">asList</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">,</span><span class="token number">3</span><span class="token punctuation">,</span><span class="token number">4</span><span class="token punctuation">,</span><span class="token number">5</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

  <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>asList<span class="token punctuation">)</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span></span></code></pre>





<h3 id="38-System"><a href="#38-System" class="headerlink" title="38. System"></a>38. System</h3><h4 id="38-1-System常见方法"><a href="#38-1-System常见方法" class="headerlink" title="38.1 System常见方法"></a>38.1 System常见方法</h4><h5 id="exit"><a href="#exit" class="headerlink" title="exit()"></a>exit()</h5><blockquote>
<p>退出当前程序</p>
</blockquote>
<p>System.exit(退出状态)；</p>
<p>0：表示正常退出。</p>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token class-name">Sysyem</span><span class="token punctuation">.</span><span class="token function">exit</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>





<h5 id="arraycopy"><a href="#arraycopy" class="headerlink" title="arraycopy()"></a>arraycopy()</h5><blockquote>
<p>复制数组元素，比较适合底层调用，一般使用Arrays.copyOf完成复制数组。(Arrays.copyOf()底层使用arraycopy()方法完成数组的复制)</p>
</blockquote>
<p>System.arraycopy(原数组,元素值索引,被拷贝数据,被拷贝数组开始索引,拷贝数量)</p>
<p>如果越界会被抛出异常。</p>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token class-name">System</span><span class="token punctuation">.</span><span class="token function">arraycopy</span><span class="token punctuation">(</span>src<span class="token punctuation">,</span><span class="token number">0</span><span class="token punctuation">,</span>dest<span class="token punctuation">,</span><span class="token number">0</span><span class="token punctuation">,</span><span class="token number">3</span><span class="token punctuation">)</span><span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>





<h5 id="currentTimeMillens"><a href="#currentTimeMillens" class="headerlink" title="currentTimeMillens"></a>currentTimeMillens</h5><blockquote>
<p>返回当前时间距离1970-1-1的毫秒数。</p>
</blockquote>
<p>System.currentTimeMillens()s</p>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">long</span> time <span class="token operator">=</span> <span class="token class-name">System</span><span class="token punctuation">.</span><span class="token function">currentTimeMillis</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>time<span class="token punctuation">)</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre>







<h5 id="gc"><a href="#gc" class="headerlink" title="gc"></a>gc</h5><blockquote>
<p>运行垃圾回收机制。</p>
<p>并不是百分之百运行垃圾回收机制。</p>
<p>尽量少用，后台调用的是full gc，严重影响gc性能。</p>
</blockquote>
<p>System.gc()</p>
<h3 id="39-BigInteger与BigDecimal"><a href="#39-BigInteger与BigDecimal" class="headerlink" title="39. BigInteger与BigDecimal"></a>39. BigInteger与BigDecimal</h3><blockquote>
<p>BigInteger适合保存比较大的整型。</p>
<p>BigDecimal适合保存精度更高的浮点数(小数)。</p>
</blockquote>
<h4 id="39-1-BigInteger"><a href="#39-1-BigInteger" class="headerlink" title="39.1 BigInteger"></a>39.1 BigInteger</h4><blockquote>
<p>在编程中可能需要处理很长的整数，long把不够用。</p>
</blockquote>
<p>BigInteger bigInteger = new BigInteger(数字);</p>
<p>BigInteger bigInteger = new BigInteger(“数字”);</p>
<p>可以向BigInteger中传入字符串，底层会将字符串转换为数据类型。</p>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token class-name">BigInteger</span> bigInteger <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">BigInteger</span><span class="token punctuation">(</span><span class="token number">123456789123456789</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token class-name">BigInteger</span> bigInteger <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">BigInteger</span><span class="token punctuation">(</span>“<span class="token number">123456789123456789</span>”<span class="token punctuation">)</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre>



<h4 id="39-2-BigInteger方法"><a href="#39-2-BigInteger方法" class="headerlink" title="39.2 BigInteger方法"></a>39.2 BigInteger方法</h4><blockquote>
<p>在BigInteger中进行加减乘除时，不能直接进行，需要使用对应的方法。</p>
</blockquote>
<p>add() 加法</p>
<p>BigInteger.add(数字)；</p>
<p>可以与BigInteger类型相加也可以和long类型相加。</p>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token class-name">BigInteger</span> bg <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">BigInteger</span><span class="token punctuation">(</span><span class="token string">"12435680987"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token class-name">BigInteger</span> bg1 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">BigInteger</span><span class="token punctuation">(</span><span class="token string">"12435680987"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  bg <span class="token operator">=</span> bg<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span>bg1<span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>bg<span class="token punctuation">)</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span></span></code></pre>





<p>subtract() 减法</p>
<p>BigInteger.subtract(数字)；</p>
<p>可以与BigInteger类型相减也可以和long类型相减。</p>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token class-name">BigInteger</span> bg <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">BigInteger</span><span class="token punctuation">(</span><span class="token string">"12435680987"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token class-name">BigInteger</span> bg1 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">BigInteger</span><span class="token punctuation">(</span><span class="token string">"12435680987"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
bg <span class="token operator">=</span> bg<span class="token punctuation">.</span><span class="token function">subtract</span><span class="token punctuation">(</span>bg1<span class="token punctuation">)</span><span class="token punctuation">;</span>   
<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>bg<span class="token punctuation">)</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span></span></code></pre>





<p>multiply() 乘法</p>
<p>BigInteger.add(数字)；</p>
<p>可以与BigInteger类型相乘也可以和long类型相乘。</p>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token class-name">BigInteger</span> bg <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">BigInteger</span><span class="token punctuation">(</span><span class="token string">"12435680987"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token class-name">BigInteger</span> bg1 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">BigInteger</span><span class="token punctuation">(</span><span class="token string">"12"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  bg <span class="token operator">=</span> bg<span class="token punctuation">.</span><span class="token function">multiply</span><span class="token punctuation">(</span>bg1<span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>bg<span class="token punctuation">)</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span></span></code></pre>





<p>divide() 除法</p>
<p>BigInteger.divide(数字)；</p>
<p>可以与BigInteger类型相加也可以和long类型相加。</p>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token class-name">BigInteger</span> bg <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">BigInteger</span><span class="token punctuation">(</span><span class="token string">"12435680987"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token class-name">BigInteger</span> bg1 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">BigInteger</span><span class="token punctuation">(</span><span class="token string">"10"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  bg <span class="token operator">=</span> bg<span class="token punctuation">.</span><span class="token function">divide</span><span class="token punctuation">(</span>bg1<span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>bg<span class="token punctuation">)</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span></span></code></pre>





<h4 id="39-3-BigDicimal"><a href="#39-3-BigDicimal" class="headerlink" title="39.3 BigDicimal"></a>39.3 BigDicimal</h4><blockquote>
<p>当需要存储精度特别大的小数时可以使用BigDecimal来存储。</p>
</blockquote>
<p>BigDicimal bigdicimal = new BigDicimal(数字);</p>
<p>BigDicimal bigdicimal = new BigDicimal(“数字”);</p>
<p>可以向BigDicimal 中传入字符串，底层会将字符串转换为数据类型。</p>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token class-name">BigDecimal</span> bigDecimal <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">BigDecimal</span><span class="token punctuation">(</span><span class="token string">"1.23242354235454"</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>





<h4 id="39-3-BigDicimal方法"><a href="#39-3-BigDicimal方法" class="headerlink" title="39.3 BigDicimal方法"></a>39.3 BigDicimal方法</h4><blockquote>
<p>不能直接对BigDicimal进行加减运算，只能调用相应的方法。</p>
</blockquote>
<p>add() 加法</p>
<p>BigDicimal.add(数字)；</p>
<p>只能与BigDicimal类型相加。</p>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token class-name">BigDicimal</span> bg <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">BigDicimal</span><span class="token punctuation">(</span><span class="token string">"1.2435680987"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token class-name">BigDicimal</span> bg1 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">BigDicimal</span><span class="token punctuation">(</span><span class="token string">"1.2435680987"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
bg <span class="token operator">=</span> bg<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span>bg1<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>bg<span class="token punctuation">)</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span></span></code></pre>





<p>subtract() 减法</p>
<p>BigDicimal.subtract(数字)；</p>
<p>只能与BigDicimal类型相减。</p>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token class-name">BigDicimal</span> bg <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">BigDicimal</span><span class="token punctuation">(</span><span class="token string">"1.2435680987"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token class-name">BigDicimal</span> bg1 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">BigDicimal</span><span class="token punctuation">(</span><span class="token string">"1.2435680987"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
bg <span class="token operator">=</span> bg<span class="token punctuation">.</span><span class="token function">subtract</span><span class="token punctuation">(</span>bg1<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>bg<span class="token punctuation">)</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span></span></code></pre>





<p>multiply() 乘法</p>
<p>BigDicimal.add(数字)；</p>
<p>只能与BigDicimal类型相乘</p>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token class-name">BigDicimal</span> bg <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">BigDicimal</span><span class="token punctuation">(</span><span class="token string">"1.2435680987"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token class-name">BigDicimal</span> bg1 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">BigDicimal</span><span class="token punctuation">(</span><span class="token string">"1.2"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
bg <span class="token operator">=</span> bg<span class="token punctuation">.</span><span class="token function">multiply</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">(</span>bg1<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>bg<span class="token punctuation">)</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span></span></code></pre>





<p>divide() 除法</p>
<p>BigDicimal.divide(数字)；</p>
<p>只能与BigDicimal类型相加也可以和long类型相加。</p>
<p>可能会抛出异常ArithmeticException，精度太高可能会一直除下去无限循环。</p>
<p>可以在调用BigDicimal.divide()方法时添加精度。如果有无限循环小数，==就会保留分子的精度。==</p>
<p>BigDicimal.divide(数字，BigDecimal.ROUND_CEILING)；</p>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token class-name">BigDicimal</span> bg <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">BigDicimal</span><span class="token punctuation">(</span><span class="token string">"1.2435680987"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token class-name">BigDicimalr</span> bg1 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">BigDicimal</span><span class="token punctuation">(</span><span class="token string">"10"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
bg <span class="token operator">=</span> bg<span class="token punctuation">.</span><span class="token function">divide</span><span class="token punctuation">(</span>bg1<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>bg<span class="token punctuation">)</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span></span></code></pre>





<h3 id="40-日期类"><a href="#40-日期类" class="headerlink" title="40. 日期类"></a>40. 日期类</h3><h4 id="40-1-Date"><a href="#40-1-Date" class="headerlink" title="40.1 Date"></a>40.1 Date</h4><p>Date:精确到毫秒，代表特定的瞬间。</p>
<p>SimpleDateFormat： 格式和解析日期的类。允许将进行格式化。</p>
<p>(日期 —&gt; 文本)(文本 —&gt; 日期)和规范化。</p>
<p>构造器</p>
<blockquote>
<p>Date类中有三个构造器</p>
</blockquote>
<p>无参构造器：</p>
<ol>
<li>返回的格式时国外的格式，通常需要对格式进行转换。</li>
<li>使用SimpleDateFormat类中的format()方法进行格式转换。</li>
<li>获取当前的系统时间。</li>
<li>在使用SimpleDateFormat类中的format()方法进行格式转换时，需要获取字符串的格式和传入SimpleDateFormat类构造器的格式一致，否则会抛出异常：ParseException.</li>
</ol>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token comment">//获取当前系统时间</span>
<span class="token class-name">Date</span> date <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Date</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>date<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">//格式转换</span>
<span class="token class-name">SimpleDateFormat</span> sdf <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">SimpleDateFormat</span><span class="token punctuation">(</span><span class="token string">"yyyy年MM月dd日 hh:mm:ss E"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token class-name">String</span> time <span class="token operator">=</span> sdf<span class="token punctuation">.</span><span class="token function">format</span><span class="token punctuation">(</span>date<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>time<span class="token punctuation">)</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>



<blockquote>
<p>毫秒数构造器</p>
</blockquote>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token comment">//通过毫秒数得到时间</span>
<span class="token class-name">Date</span> date <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Date</span><span class="token punctuation">(</span>毫秒数<span class="token punctuation">)</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre>



<p>字符串构造器</p>
<ol>
<li>可以将一个格式化的String转换成对应的Date</li>
<li>得到的Date仍然在输出时还是国外的形式。</li>
</ol>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token class-name">String</span> time <span class="token operator">=</span> <span class="token string">"Wed Mar 02 19:43:55 CST 2022"</span><span class="token punctuation">;</span>
<span class="token class-name">Date</span> date <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Date</span><span class="token punctuation">(</span>time<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">//格式转换</span>
<span class="token class-name">SimpleDateFormat</span> sdf <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">SimpleDateFormat</span><span class="token punctuation">(</span><span class="token string">"yyyy年MM月dd日 hh:mm:ss E"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token class-name">String</span> times <span class="token operator">=</span> sdf<span class="token punctuation">.</span><span class="token function">format</span><span class="token punctuation">(</span>date<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>times<span class="token punctuation">)</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>









<h4 id="40-2-Calendar类"><a href="#40-2-Calendar类" class="headerlink" title="40.2 Calendar类"></a>40.2 Calendar类</h4><blockquote>
<p>Calendar类是一个抽象类，它为特定瞬间与一组诸如YEAR，MONTH，</p>
</blockquote>
<p>DAR_OF_MONTH,HOUR等日历字段之间的转换提供了一些方法，并为操作日历字段提供了一些方法。</p>
<p>Calendar的==构造器是private，可以通过getInstance()获取。==</p>
<p>Calendar提供了大量方法和字段提供调用。</p>
<p>==返回月份时按照0开始编号。==</p>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token class-name">Calendar</span> cale <span class="token operator">=</span> <span class="token class-name">Calendar</span><span class="token punctuation">.</span><span class="token function">getInstance</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">//获取日历对象的某个日历字段</span>
<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"年:"</span> <span class="token operator">+</span> cale<span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token class-name">Calendar</span><span class="token punctuation">.</span><span class="token constant">YEAR</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">//返回月份时，是 按照0编号的，需要返回值加一</span>
<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"月:"</span> <span class="token operator">+</span> cale<span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token class-name">Calendar</span><span class="token punctuation">.</span><span class="token constant">MONTH</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"日:"</span> <span class="token operator">+</span> cale<span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token class-name">Calendar</span><span class="token punctuation">.</span><span class="token constant">DAY_OF_MONTH</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">//返回以12进制小时数</span>
<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"小时:"</span> <span class="token operator">+</span> cale<span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token class-name">Calendar</span><span class="token punctuation">.</span><span class="token constant">HOUR</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"分钟:"</span> <span class="token operator">+</span> cale<span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token class-name">Calendar</span><span class="token punctuation">.</span><span class="token constant">MINUTE</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"秒:"</span> <span class="token operator">+</span> cale<span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token class-name">Calendar</span><span class="token punctuation">.</span><span class="token constant">SECOND</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">//返回以24进制小时数</span>
<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"小时:"</span> <span class="token operator">+</span> cale<span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token class-name">Calendar</span><span class="token punctuation">.</span><span class="token constant">HOUR_OF_DAY</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>



<p>缺点：</p>
<ol>
<li>可变性：像日期和时间这样的类应该是不可变的。</li>
<li>偏移性：date中的年份从1900年开始，月份都是从零开始。</li>
<li>格式化: 格式化只对Date有用，Calendar则不行。</li>
<li>Date和Calender都不是线程安全的，不能处理闰秒。(每隔两天，多出1s)</li>
</ol>
<h4 id="40-3-LocalDate，LocalTime，LocalDateTime"><a href="#40-3-LocalDate，LocalTime，LocalDateTime" class="headerlink" title="40.3 LocalDate，LocalTime，LocalDateTime"></a>40.3 LocalDate，LocalTime，LocalDateTime</h4><p>LocalDate返回日期，可以获取日期字段。</p>
<p>LocalTime返回时间，可以获取时间字段。</p>
<p>LocalDateTime返回日期时间，可以获取日期和时间字段。</p>
<p>这些时间类是在JDK8中加入的。</p>
<ul>
<li>使用plus方法测试增加时间的某个日期。</li>
</ul>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token class-name">LocalDateTime</span> ldt <span class="token operator">=</span> <span class="token class-name">LocalDateTime</span><span class="token punctuation">.</span><span class="token function">now</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token class-name">DateTimeFormatter</span> dtf <span class="token operator">=</span> <span class="token class-name">DateTimeFormatter</span><span class="token punctuation">.</span><span class="token function">ofPattern</span><span class="token punctuation">(</span><span class="token string">"yyyy年MM月dd日 HH时mm分钟ss秒"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">//获取指定天数后的对象</span>
<span class="token class-name">LocalDateTime</span> ldt2 <span class="token operator">=</span> ldt<span class="token punctuation">.</span><span class="token function">plusDays</span><span class="token punctuation">(</span><span class="token number">875</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">//转换格式</span>
<span class="token class-name">String</span> format <span class="token operator">=</span> dtf<span class="token punctuation">.</span><span class="token function">format</span><span class="token punctuation">(</span>ldt2<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>format<span class="token punctuation">)</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<ul>
<li>使用minus方法测试查看减少时间的某个日期</li>
</ul>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token class-name">LocalDateTime</span> ldt <span class="token operator">=</span> <span class="token class-name">LocalDateTime</span><span class="token punctuation">.</span><span class="token function">now</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token class-name">DateTimeFormatter</span> dtf <span class="token operator">=</span> <span class="token class-name">DateTimeFormatter</span><span class="token punctuation">.</span><span class="token function">ofPattern</span><span class="token punctuation">(</span><span class="token string">"yyyy年MM月dd日 HH时mm分钟ss秒"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">//获取指定天数前的对象</span>
<span class="token class-name">LocalDateTime</span> ldt2 <span class="token operator">=</span> ldt<span class="token punctuation">.</span><span class="token function">minusDays</span><span class="token punctuation">(</span><span class="token number">875</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">//转换格式</span>
<span class="token class-name">String</span> format <span class="token operator">=</span> dtf<span class="token punctuation">.</span><span class="token function">format</span><span class="token punctuation">(</span>ldt2<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>format<span class="token punctuation">)</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>



<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token comment">//获取日期和时间字段。</span>
<span class="token class-name">LocalDateTime</span> ldt <span class="token operator">=</span> <span class="token class-name">LocalDateTime</span><span class="token punctuation">.</span><span class="token function">now</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">//获取日期字段。</span>
<span class="token class-name">LocalDate</span> ld <span class="token operator">=</span> <span class="token class-name">LocalDate</span><span class="token punctuation">.</span><span class="token function">now</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">//获取时间字段。</span>
<span class="token class-name">LocalTime</span> lt <span class="token operator">=</span> <span class="token class-name">LocalTime</span><span class="token punctuation">.</span><span class="token function">now</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"年:"</span>  <span class="token operator">+</span> ldt<span class="token punctuation">.</span><span class="token function">getYear</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"月(英文):"</span>  <span class="token operator">+</span> ldt<span class="token punctuation">.</span><span class="token function">getMonth</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"月(数字):"</span>  <span class="token operator">+</span> ldt<span class="token punctuation">.</span><span class="token function">getMonthValue</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"日:"</span>  <span class="token operator">+</span> ldt<span class="token punctuation">.</span><span class="token function">getDayOfMonth</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"时:"</span>  <span class="token operator">+</span> ldt<span class="token punctuation">.</span><span class="token function">getHour</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"分:"</span>  <span class="token operator">+</span> ldt<span class="token punctuation">.</span><span class="token function">getMinute</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"秒:"</span>  <span class="token operator">+</span> ldt<span class="token punctuation">.</span><span class="token function">getSecond</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>



<blockquote>
<p>==可以使用DateTimeFormatter对象的ofPattern方法==来进行格式化。类似于SimpleDateFormat。</p>
</blockquote>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token class-name">LocalDateTime</span> ldt <span class="token operator">=</span> <span class="token class-name">LocalDateTime</span><span class="token punctuation">.</span><span class="token function">now</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token class-name">DateTimeFormatter</span> dtf <span class="token operator">=</span> <span class="token class-name">DateTimeFormatter</span><span class="token punctuation">.</span><span class="token function">ofPattern</span><span class="token punctuation">(</span><span class="token string">"yyyy年MM月dd日 HH时mm分钟ss秒"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token class-name">String</span> format <span class="token operator">=</span> dtf<span class="token punctuation">.</span><span class="token function">format</span><span class="token punctuation">(</span>ldt<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>format<span class="token punctuation">)</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span></span></code></pre>



<p>时间戳</p>
<blockquote>
<p>类似于Date，提供了一系列和Date类转换的方式。</p>
<p>Instant —-&gt; Date</p>
</blockquote>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token class-name">Instant</span> instant <span class="token operator">=</span> <span class="token class-name">Instant</span><span class="token punctuation">.</span><span class="token keyword">new</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token class-name">Date</span> date <span class="token operator">=</span> <span class="token class-name">Date</span><span class="token punctuation">.</span><span class="token function">from</span><span class="token punctuation">(</span>instant<span class="token punctuation">)</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre>



<blockquote>
<p>Date —&gt; Instant</p>
</blockquote>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token class-name">Date</span> date <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Date</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token class-name">Instant</span> instant <span class="token operator">=</span> date<span class="token punctuation">.</span><span class="token function">toInstant</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre>





<h3 id="41-集合"><a href="#41-集合" class="headerlink" title="41. 集合"></a>41. 集合</h3><blockquote>
<p>保存数据可以使用数组也可以使用集合。</p>
</blockquote>
<p>数组：</p>
<ul>
<li>长度开始时必须指定，而且一旦指定，不能更改。</li>
<li>保存的必须为同一类型的元素。</li>
<li>使用数组进行增加元素比较麻烦。(创建新的数组，然后将原本数组复制到新的数组)</li>
</ul>
<p>集合：</p>
<ul>
<li>可以动态保存任意多个对象，使用比较方便。</li>
<li>提供了一系列方便的操作对象方法：add，remove，set，get。</li>
<li>集合主要是两组(单列集合，双列集合)</li>
<li>Collection接口有两个重要的子接口List，Set，他们的实现子类都是单列集合。</li>
<li>Map接口的实现子类是双列集合，存放的键值对形式。</li>
</ul>
<h4 id="41-1-集合继承图"><a href="#41-1-集合继承图" class="headerlink" title="41.1 集合继承图"></a>41.1 集合继承图</h4><p>集合主要是两组(单列集合，双列集合)</p>
<p>Collection接口有两个重要的子接口List，Set，他们的实现子类都是单列集合。</p>
<p>Map接口的实现子类是双列集合，存放的键值对形式。</p>
<h4 id="41-2-Collection接口"><a href="#41-2-Collection接口" class="headerlink" title="41.2 Collection接口"></a>41.2 Collection接口</h4><p>Collection接口的实现子类可以存放多个元素，每个元素可以是Object。</p>
<p>有些Collection的实现类，可以存放重复的元素，有些不可以。</p>
<p>有些Collection的实现类，是有序的(list),有些不是有序的(set)。</p>
<p>Collection接口没有直接的实现子类，是通过它的子接口Set和List来实现。</p>
<p>接口中的方法：</p>
<table>
<thead>
<tr>
<th>方法</th>
<th>解释</th>
<th></th>
</tr>
</thead>
<tbody><tr>
<td>add()</td>
<td>添加单个元素(其中元素为对象)</td>
<td>放置==基本数据类型自动装箱==</td>
</tr>
<tr>
<td>remove(索引\值)</td>
<td>删除指定元素</td>
<td></td>
</tr>
<tr>
<td>contains(值)</td>
<td>查找元素是否存在</td>
<td></td>
</tr>
<tr>
<td>size()</td>
<td>获取元素个数</td>
<td></td>
</tr>
<tr>
<td>isEmpty()</td>
<td>判断是否为空</td>
<td></td>
</tr>
<tr>
<td>clear()</td>
<td>清空</td>
<td></td>
</tr>
<tr>
<td>addAll(集合)</td>
<td>添加多个元素</td>
<td>可以添加实现了Collection接口的集合</td>
</tr>
<tr>
<td>containsAll(集合)</td>
<td>查找多个元素是否都存在</td>
<td>可以添加实现了Collection接口的集合</td>
</tr>
<tr>
<td>removeAll(集合)</td>
<td>删除多个元素</td>
<td>可以添加实现了Collection接口的集合</td>
</tr>
</tbody></table>
<p>Collection遍历元素方式</p>
<blockquote>
<p>Iterator对象称为迭代器，主要用于遍历Collection集合中的元素。</p>
</blockquote>
<p>==所有实现了Collection接口的集合都有一个iterator()方法，==可以返回一个实现了Iterator接口的对象，即可以返回一个迭代器。</p>
<p>Iterator仅仅用于遍历集合，==Iterator本身并不存放对象==。</p>
<p>Iterator细节：</p>
<ul>
<li>使用迭代器得到下一个元素之前必须使用迭代器的hasNext方法判断是否有下一个元素。</li>
<li>如果没有使用hasNext方法判断，调用Next方法且已经没有下一个元素会抛出异常：NoSuchElementException。</li>
<li>可以使用iterator.Next()将指向下移并且得到当前指向的元素。</li>
<li>当退出while循环后，这是iterator迭代器还是指向最后的元素，不能再次调用Next，否则会指向超出集合长度，抛出异常：NoSuchElementException。</li>
<li>==如果希望再次遍历，可以重新得到迭代器==，不能使用原有迭代器：</li>
</ul>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token class-name">ArrayList</span> arrayList <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ArrayList</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">//第一次构造迭代器</span>
<span class="token class-name">Iterator</span> iterator <span class="token operator">=</span> arrayList<span class="token punctuation">.</span><span class="token function">iterator</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">//重新构造迭代器</span>
iterator <span class="token operator">=</span> arrayList<span class="token punctuation">.</span><span class="token function">iterator</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span></span></code></pre>



<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token class-name">ArrayList</span> arrayList <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ArrayList</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
arrayList<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span><span class="token string">"元素"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//其中有自动装箱</span>
<span class="token comment">//得到迭代器</span>
<span class="token class-name">Iterator</span> iterator <span class="token operator">=</span> arrayList<span class="token punctuation">.</span><span class="token function">iterator</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">//使用迭代器得到下一个元素之前必须使用迭代器的hasNext方法判断是否有下一个元素</span>
<span class="token comment">//如果没有使用hasNext方法判断，调用Next方法且已经没有下一个元素会抛出异常：NoSuchElementException</span>
<span class="token comment">//</span>
<span class="token keyword">while</span> <span class="token punctuation">(</span>iterator<span class="token punctuation">.</span><span class="token function">hasNext</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
		<span class="token comment">//</span>
 <span class="token class-name">Object</span> next <span class="token operator">=</span> iterator<span class="token punctuation">.</span><span class="token function">next</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>



<p>增强for</p>
<p>增强for循环，可以替代iterator迭代器，增强for就是简化版的iterator，本质一样们==只能用于遍历集合或者数组。==</p>
<p>==增强for的底层也是使用了迭代器==。</p>
<p>语法：for(元素类型 元素名 : 集合名或数组 ){ 访问元素 }</p>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token class-name">ArrayList</span> arrayList <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ArrayList</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
arrayList<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span><span class="token string">"元素"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//其中有自动装箱</span>

  <span class="token keyword">for</span><span class="token punctuation">(</span><span class="token class-name">Object</span> ob <span class="token operator">:</span> arrayList<span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
      <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>ob<span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>



<p>普通for</p>
<p>也可以使用普通for循环进行遍历集合</p>
<p>普通for循环只能用作有顺序集合的遍历</p>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token class-name">ArrayList</span> arrayList <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ArrayList</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
arrayList<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span><span class="token string">"元素"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//其中有自动装箱</span>

  <span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>i <span class="token operator">&lt;</span> arrayList<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>i<span class="token operator">++</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
      <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>arrayList<span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span>i<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>





<h4 id="41-3-List-接口"><a href="#41-3-List-接口" class="headerlink" title="41.3 List 接口"></a>41.3 List 接口</h4><blockquote>
<p>List接口是Collection接口的子接口。</p>
</blockquote>
<ul>
<li>List集合中的元素有序(即添加顺序和取出顺序一致)，且可重复。</li>
<li>List集合中的每个元素都有其对应的顺序索引，即支持索引。</li>
<li>List容器中的元素都对应一个整数型的序号记载其在容器中的位置，==可以根据序号存取容器中的元素==。</li>
</ul>
<p>List常用方法:</p>
<p>add() 添加</p>
<blockquote>
<p>add(索引,元素);</p>
<p>向指定索引处添加指定元素，如果不指定索引默认添加到最后。</p>
</blockquote>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token class-name">List</span> arrayList <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ArrayList</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
arrayList<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span><span class="token string">"元素"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
arrayList<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span><span class="token string">"索引0元素"</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span></span></code></pre>





<p>addAll() 添加</p>
<blockquote>
<p>addAll(集合);</p>
<p>向指定索引处添加指定所有元素(集合)，如果不指定索引默认添加到最后。</p>
</blockquote>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token class-name">List</span> arrayList <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ArrayList</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token class-name">List</span> arrayList1 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ArrayList</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
arrayList<span class="token punctuation">.</span><span class="token function">addAll</span><span class="token punctuation">(</span>arrayList1<span class="token punctuation">)</span><span class="token punctuation">;</span>
arrayList<span class="token punctuation">.</span><span class="token function">addAll</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span>arrayList1<span class="token punctuation">)</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span></span></code></pre>





<p>get() 获得</p>
<blockquote>
<p>get(索引);</p>
<p>返回指定索引位置的元素。</p>
<p>如果超出索引长度会抛出异常。</p>
</blockquote>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token class-name">List</span> arrayList <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ArrayList</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

arrayList<span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span></span></code></pre>





<p>indexOf() 查找</p>
<blockquote>
<p>indexOf(元素);</p>
<p>返回指定元素在当前集合中首次出现的位置索引。</p>
</blockquote>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token class-name">List</span> arrayList <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ArrayList</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

arrayList<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span><span class="token string">"元素"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
arrayList<span class="token punctuation">.</span><span class="token function">indexOf</span><span class="token punctuation">(</span><span class="token string">"元素"</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span></span></code></pre>





<p>laseindexOf() 添加</p>
<blockquote>
<p>laseindexOf()(元素);</p>
<p>返回指定元素在当前集合中最后出现的位置索引。</p>
</blockquote>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token class-name">List</span> arrayList <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ArrayList</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

arrayList<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span><span class="token string">"元素"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
arrayList<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span><span class="token string">"元素"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
arrayList<span class="token punctuation">.</span><span class="token function">laseindexOf</span><span class="token punctuation">(</span><span class="token string">"元素"</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span></span></code></pre>





<p>remove() 删除</p>
<blockquote>
<p> remove(索引)；</p>
<p> 移除指定index位置的元素，并且返回此元素。</p>
</blockquote>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token class-name">List</span> arrayList <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ArrayList</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

arrayList<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span><span class="token string">"元素"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token class-name">String</span> str <span class="token operator">=</span>  arrayList<span class="token punctuation">.</span><span class="token function">remove</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span></span></code></pre>





<p>set() 设置(替换)</p>
<blockquote>
<p>set(索引,元素)；</p>
<p>设置指定索引位置为指定元素，相当与替换。</p>
</blockquote>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token class-name">List</span> arrayList <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ArrayList</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

arrayList<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span><span class="token string">"元素"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
arrayList<span class="token punctuation">.</span><span class="token function">set</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span><span class="token string">"元素"</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span></span></code></pre>





<p>subList()</p>
<blockquote>
<p>subList(开始索引，结束索引)；</p>
<p>截取指定范围的集合元素返回一个新的集合，包含开始索引，不包含结束索引。</p>
</blockquote>
<pre class="line-numbers language-java'" data-language="java'"><code class="language-java'">List arrayList &#x3D; new ArrayList();

arrayList.add(&quot;元素0&quot;);
arrayList.add(&quot;元素1&quot;);
arrayList.add(&quot;元素2&quot;);
&#x2F;&#x2F;获取第零个元素和第一个元素
List arrayList2 &#x3D; arrayList.subList(0,2);<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>



<h4 id="41-4-ArrayList"><a href="#41-4-ArrayList" class="headerlink" title="41.4 ArrayList"></a>41.4 ArrayList</h4><p>ArrayList可以加入Null，并且多个。</p>
<p>ArrayList由数组实现数据的存储。</p>
<p>ArrayList基本等同于Vector，除了ArrayList是线程不安全的(执行效率高)在多线程情况下，不建议使用ArrayList。多线程推荐使用Vector。(ArrayList没有使用synchronized修饰)</p>
<p>分析：</p>
<ul>
<li>ArrayList中==维护了一个Object类型的数组elementData。==</li>
</ul>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">transient</span> <span class="token class-name">Object</span><span class="token punctuation">[</span><span class="token punctuation">]</span> elementData<span class="token punctuation">;</span>  <span class="token comment">//transient表示短暂的，瞬间，保证该属性不会被序列化。</span><span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>

<ul>
<li>当创建对象时，如果使用的是无参构造器，则初始elementData的容量为0.(jdk 8为10)</li>
<li>当添加元素时，先判断是否需要扩容，如果需要扩容，则调用grow方法，否则直接添加元素到合适的位置。</li>
<li>如果使用的是无参构造器，如果是第一次添加，需要扩容的话，扩容后的elementData为10，如果需要再次扩容的话，则扩容elementData为1.5倍。(第一次为10，第二次为15)</li>
<li>如果使用的是指定容量capacity的构造器，则初始elementData容量为caoacity。</li>
<li>如果使用的是只当容量capacity的构造器，如果需要扩容，则直接扩容elementData为1.5倍。</li>
</ul>
<h4 id="41-5-Vector"><a href="#41-5-Vector" class="headerlink" title="41.5 Vector"></a>41.5 Vector</h4><p>Vector底层也是一个对象数组，Protected Object[] elementData；</p>
<p>Vector的对象是有固定顺序且可以重复的。</p>
<p>Vector是线程同步的，即线程安全，Vector类的操作方法带有synchronized。</p>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">public</span> <span class="token keyword">synchronized</span> <span class="token class-name">E</span> <span class="token function">get</span><span class="token punctuation">(</span><span class="token keyword">int</span> index<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
  <span class="token keyword">if</span> <span class="token punctuation">(</span>index <span class="token operator">>=</span> elementCount<span class="token punctuation">)</span>
      <span class="token keyword">throw</span> <span class="token keyword">new</span> <span class="token class-name">ArrayIndexOutOfBoundsException</span><span class="token punctuation">(</span>index<span class="token punctuation">)</span><span class="token punctuation">;</span>

  <span class="token keyword">return</span> <span class="token function">elementData</span><span class="token punctuation">(</span>index<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>在开发中，需要线程同步安全时，考虑使用Vector。</p>
<table>
<thead>
<tr>
<th></th>
<th>底层结构</th>
<th>版本</th>
<th>同步，效率</th>
<th>扩容倍数</th>
</tr>
</thead>
<tbody><tr>
<td>ArrayList</td>
<td>可变数组</td>
<td>jdk1.2</td>
<td>不安全，效率高</td>
<td>如果有参数构造为传入参数大小，扩容1.5倍；无参数默认是0，第一次10，第二次1.5倍</td>
</tr>
<tr>
<td>Vector</td>
<td>可变数组</td>
<td>jdk1.0</td>
<td>安全，效率低</td>
<td>如果是无参，默认10，满后按照2倍扩容，如果指定大小，则每次直接按照2倍扩容</td>
</tr>
</tbody></table>
<h4 id="41-6-LinkedList"><a href="#41-6-LinkedList" class="headerlink" title="41.6 LinkedList"></a>41.6 LinkedList</h4><p>LinkedList实现了双向链表和双端队列特点。</p>
<p>可以添加任意元素(元素可以重复),包括null。</p>
<p>线程不安全，没有实现同步。</p>
<ul>
<li>LinkedList底层维护了一个双向链表。</li>
<li>LinkedList中维护了两个属性first和last分别指向首节点和尾节点。</li>
<li>每个节点(Node对象)，里面又维护了prev，next，item三个属性，其中通过prev指向前一个，通过next指向后一个节点，最终实现双向链表。</li>
<li>所以LinkedList的元素的添加和删除，不是通过数组完成的，相对来说效率较高。</li>
</ul>
<h5 id="LinkedListCRUD"><a href="#LinkedListCRUD" class="headerlink" title="LinkedListCRUD"></a>LinkedListCRUD</h5><p>add()</p>
<blockquote>
<p>add(元素)；</p>
<p>向指定LinkedList对象中添加元素。</p>
</blockquote>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token class-name">LinkedList</span> lkl <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">LinkedList</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator">&lt;</span> <span class="token number">10</span><span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
        lkl<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span>i<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span></span></code></pre>

<p>remove()删除</p>
<blockquote>
<p>remove();   remove(节点位置);</p>
<p>remove默认删除的是第一个节点，可以指定删除哪一个节点。</p>
</blockquote>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token class-name">LinkedList</span> lkl <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">LinkedList</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator">&lt;</span> <span class="token number">10</span><span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
      lkl<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span>i<span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token punctuation">&#125;</span>
  lkl<span class="token punctuation">.</span><span class="token function">remove</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	    lkl<span class="token punctuation">.</span><span class="token function">remove</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>lkl<span class="token punctuation">)</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>







<p>set() 设置</p>
<blockquote>
<p>set(节点位置，元素);    （节点索引也是从零开始）</p>
<p>将指定节点位置的元素更换为指定元素。</p>
</blockquote>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token class-name">LinkedList</span> lkl <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">LinkedList</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator">&lt;</span> <span class="token number">10</span><span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
      lkl<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span>i<span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token punctuation">&#125;</span>
  lkl<span class="token punctuation">.</span><span class="token function">set</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">9999</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>lkl<span class="token punctuation">)</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>





<p>get() 获取</p>
<blockquote>
<p>get(节点位置)；</p>
<p>获取指定节点的位置。</p>
</blockquote>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token class-name">LinkedList</span> lkl <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">LinkedList</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator">&lt;</span> <span class="token number">10</span><span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
      lkl<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span>i<span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token punctuation">&#125;</span>
  <span class="token class-name">Object</span> obj <span class="token operator">=</span> lkl<span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>obj<span class="token punctuation">)</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>





<table>
<thead>
<tr>
<th></th>
<th>底层结构</th>
<th>增删的效率</th>
<th>改查的效率</th>
</tr>
</thead>
<tbody><tr>
<td>ArrayList</td>
<td>可变数组</td>
<td>较低，数组扩容</td>
<td>较高</td>
</tr>
<tr>
<td>LinkedList</td>
<td>双向链表</td>
<td>较高，通过链表追加</td>
<td>较低</td>
</tr>
</tbody></table>
<ul>
<li>如果改查的操作比较多，选择ArrayList。</li>
<li>如果我们该删的操作多，选择LinkedList。</li>
<li>一般来说，在程序中，80%-90%都是查询，因此大部分情况下会选择ArrayList。<ul>
<li>允许在一个项目中，根据业务灵活选择，也可能这样，一个模块使用的是ArrayList,另一个模块是LinkedList。</li>
</ul>
</li>
</ul>
<h4 id="41-7-Set接口"><a href="#41-7-Set接口" class="headerlink" title="41.7 Set接口"></a>41.7 Set接口</h4><blockquote>
<p>无序(添加和取出来的顺序不一致，但是取出来的顺序是固定的)，所以没有索引。</p>
<p>如果添加的元素有重复只会存储一个。</p>
<p>不允许重复元素，所以最多包含一个null。</p>
<p>JDK API中set的实现类为：HashSet ，TreeSet。</p>
<p>Set和List接口一样，Set接口也是Collection的子接口，因此，常用方法和Collection的接口一样。</p>
</blockquote>
<h5 id="set接口的遍历方式"><a href="#set接口的遍历方式" class="headerlink" title="set接口的遍历方式"></a>set接口的遍历方式</h5><p>同Collection的遍历遍历一样，因为Set接口是Collection接口的子接口：</p>
<ol>
<li>可以使用迭代器</li>
</ol>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token class-name">HashSet</span> hashSet <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">HashSet</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator">&lt;</span> <span class="token number">10</span><span class="token punctuation">;</span>i<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
      hashSet<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span>i<span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token punctuation">&#125;</span>
  <span class="token class-name">Iterator</span> iterator <span class="token operator">=</span> hashSet<span class="token punctuation">.</span><span class="token function">iterator</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token keyword">while</span> <span class="token punctuation">(</span>iterator<span class="token punctuation">.</span><span class="token function">hasNext</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
      <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>iterator<span class="token punctuation">.</span><span class="token function">next</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<ol start="2">
<li>增强for</li>
<li>==不能使用索引的方式来获取元素==，因为set接口不支持索引，没有顺序。</li>
</ol>
<h4 id="41-8-HashSet"><a href="#41-8-HashSet" class="headerlink" title="41.8 HashSet"></a>41.8 HashSet</h4><p>HashSet实现了Set接口。</p>
<p>==HashSet实际上是HashMap==。(只是将HashMap的value处放固定值)</p>
<p>HashSet不允许重复元素。可以存放null值，但是只能有一个null。</p>
<p>HashSet不保证元素是有序的，取决与hash后，再确定索引的结果。</p>
<p>不能有重复元素/对象，再前面Set接口使用已经讲过。</p>
<ul>
<li>不能加入相同对象的真正含义：<ul>
<li>HashSet底层是HashMap,维护了一个table和双链表。</li>
<li>添加一个元素时，先得到hash值，会转成索引值。</li>
<li>找到保存数据表table，看这个索引位置是否已经存放有元素。</li>
<li>如果没有，直接加入，如果有调用equals比较，如果相同，就放弃添加，如果不同，则添加到最后。</li>
<li>再java8中，如果一条链表的元素个数到达TREEIFY_THRESHOLD(默认是8)并且table的大小&gt;=MIN_CAPACITY(默认是64)，就会进行树化(红黑树)</li>
<li>可以重写hashCode方法和equlas方法来控制不能重复的范围。</li>
</ul>
</li>
<li>相同元素的定义：<ul>
<li>准备加入的和已经加入的节点是同一个对象。</li>
<li>准备加入的节点和已经加入的执行equals()方法。</li>
<li>==重点是指定对象equals方法被重写后的机制==</li>
</ul>
</li>
</ul>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token class-name">HashSet</span> hashSet <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">HashSet</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
hashSet<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span><span class="token string">"字符串"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
hashSet<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span><span class="token string">"字符串"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//同一个对象添加失败</span>
hashSet<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">Dog</span><span class="token punctuation">(</span><span class="token string">"doubao"</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
hashSet<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">Dog</span><span class="token punctuation">(</span><span class="token string">"doubao"</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>  <span class="token comment">//非同一个对象添加成功</span>
hashSet<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">String</span><span class="token punctuation">(</span><span class="token string">"字符串"</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
hashSet<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">String</span><span class="token punctuation">(</span><span class="token string">"字符串"</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>



<p>HashSet的底层机制：</p>
<ul>
<li>HashSet底层是HashMap，==第一次添加时，table数组扩容到16==，临界值(threshold)是0.75 = 12.（并不是到达极限再扩容）<ul>
<li>只要数组和链表中加入一个元素就算做加一，加到临界值会触发扩容。</li>
</ul>
</li>
<li>如果table数组使用到了临界值12，就会扩容到16*2 = 32 ，新的临界值就是32 * 0.75 = 24，以此类推。</li>
<li>再java8中，如果一条链表的元素个数到达TREEIFY_THRESHOLD(默认是8)并且table的大小&gt;=MIN_CAPACITY(默认是64)，就会进行树化(红黑树)，否则仍然采用数组的扩容机制。</li>
<li>==如果单条链的长度到达8且数组长度未到达64，则会将数组按照规则扩容，每次这条链多追加元素一便扩容一次==。</li>
</ul>
<p>add()</p>
<blockquote>
<p>添加指定对象，返回一个boolean的值。</p>
<p>如果添加成功返回true，如果添加失败返回false。</p>
</blockquote>
<p>remove()</p>
<blockquote>
<p>remove(对象)；</p>
<p>删除指定对象。</p>
</blockquote>
<h4 id="41-9-LinkedHashSet"><a href="#41-9-LinkedHashSet" class="headerlink" title="41.9  LinkedHashSet"></a>41.9  LinkedHashSet</h4><p>LinkedHashSet 时HashSet的子类。实现了有顺序的存储。</p>
<p>==LinkedHashSet底层是一个LinkedHashMap==，是HashMap的子类，底层维护了一个数组+双向链表。</p>
<p>LinkedHashSet根据元素的hashCode值来决定元素的存储位置，同时用链表维护元素的次序，这使得元素看起来是以处插入顺序保存的。</p>
<p>LinkedHashSet不允许出现重复元素。</p>
<ul>
<li><p>在LinkedHashSet中维护了一个hash表和双向链表(LinkedHashSet有head和tail)</p>
</li>
<li><p>每一个节点有before和after属性，这样可以形成双向链表。</p>
</li>
<li><p>在添加一个元素时。先求hash值，再求索引，确定该元素再hashtable的位置，然后将添加的元素加入到双向链表(如果已经存在，不添加)</p>
</li>
<li><p>这样的话，遍历时可以确保插入顺序和遍历顺序一致。</p>
</li>
<li><p>添加第一次时，直接将数组table扩容到16，存放的节点类型是LinkedHashMap$Entry。</p>
</li>
<li><p>底层维护着一个HashMap$Node[]，存放的元素是LinkedHashMap$Entry类型。(多态数组)</p>
</li>
</ul>
<h4 id="41-10-TreeSet"><a href="#41-10-TreeSet" class="headerlink" title="41.10 TreeSet"></a>41.10 TreeSet</h4><blockquote>
<p>可以使用TreeSet集合将添加元素==按照指定顺序排列==；</p>
</blockquote>
<ol>
<li>如果使用无参构造器创建TreeSet时，仍然时无顺序的。但是也会去除重复，使用添加元素实现的Compareable接口的compareTo去重。<ol>
<li>如果使用无参构造器且传入的实现类并没有实现对应compareTo方法，则会在底层报错：ClassCastExcepotion。</li>
</ol>
</li>
<li>使用提供的传入一个比较器(匿名内部类)时可以按照指定顺序排序。</li>
</ol>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token class-name">TreeSet</span> set <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">TreeSet</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">Comparator</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
  <span class="token annotation punctuation">@Override</span>
  <span class="token keyword">public</span> <span class="token keyword">int</span> <span class="token function">compare</span><span class="token punctuation">(</span><span class="token class-name">Object</span> o1<span class="token punctuation">,</span> <span class="token class-name">Object</span> o2<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
    
    <span class="token comment">//根据字符串大小排序</span>
      <span class="token keyword">return</span> <span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">)</span>o1<span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">compareTo</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">)</span>o2<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token comment">//根据字符串长度排序</span>
    <span class="token comment">//return ((String)o1).length - ((String)o2).length;</span>
  <span class="token punctuation">&#125;</span>
<span class="token punctuation">&#125;</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>



<blockquote>
<p>TreeSet的底层就是TreeMap。</p>
<p>构造器会将出传入的比较器传入到TreeMap对象。当调用add()方法时，会执行传入的比较器，然后根据返回值存放元素。</p>
</blockquote>
<pre class="line-numbers language-java" data-language="java"><code class="language-java">parent <span class="token operator">=</span> t<span class="token punctuation">;</span>
    cmp <span class="token operator">=</span> cpr<span class="token punctuation">.</span><span class="token function">compare</span><span class="token punctuation">(</span>key<span class="token punctuation">,</span> t<span class="token punctuation">.</span>key<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>cmp <span class="token operator">&lt;</span> <span class="token number">0</span><span class="token punctuation">)</span>
        t <span class="token operator">=</span> t<span class="token punctuation">.</span>left<span class="token punctuation">;</span>
    <span class="token keyword">else</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>cmp <span class="token operator">></span> <span class="token number">0</span><span class="token punctuation">)</span>
        t <span class="token operator">=</span> t<span class="token punctuation">.</span>right<span class="token punctuation">;</span>
    <span class="token keyword">else</span>
        <span class="token keyword">return</span> t<span class="token punctuation">.</span><span class="token function">setValue</span><span class="token punctuation">(</span>value<span class="token punctuation">)</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>









<h4 id="41-11-Map接口"><a href="#41-11-Map接口" class="headerlink" title="41.11 Map接口"></a>41.11 Map接口</h4><blockquote>
<p>Map与Collection并列存在(其中并没有关系)，用于保存具有映射关系的数据:Key-Value。</p>
<p>==Map对象底层也维护着table和一个双链表Node。==</p>
</blockquote>
<p>Map中的key和value可以时任何引用类型的数据，会封装到HashMap$Node对象中。</p>
<p>Map中的key不允许重复。(原因和HashSet一样，HashSet也是key-value结构但是value是固定值)</p>
<p>Map中的value可以重复，可以为空和重复。</p>
<p>Map中的Key可以为null，value也可以为null，注意key为null，只能有一个。value为null，可以多个。如果有多个相同的key会采取替换机制。</p>
<p>==常用String类作为Map的key,可以使用object类型作为Key==。</p>
<p>key和value之间存在单项一对一关系，通过指定的key总能找到对应的value。但是value无法找到key。</p>
<p>Map存放数据的key-value，一对key-value是放在一个HashMap$Node中的，是因为Node实现了Entry接口。 </p>
<ul>
<li>key-value最后是HashMap$Node node = newNode(hash,key,value,null);</li>
<li>key-value为了方便遍历，还会创建EntrySet集合，该集合存放的元素的类型Entry，而一个Entry对象就有key,value,EntrySet&lt;Entry&lt;K,V&gt;&gt; entrySet;</li>
<li>EntrySet 中，定义的类型是Map.Entry,但是实际上存放的还是HashMap$Node,这是因为<code>static class Node&lt;K,Y&gt; implements Map.Entry&lt;K,V&gt;</code>利用接口的多态。</li>
<li>当把HashMap$Node对象存放到EntrySet就方便遍历，因为Entry接口提供了重要方法：<code>getKey</code>和 <code>getValue</code></li>
</ul>
<ul>
<li>Entry中存储着指定数据在HashMap$Node中的地址，实际上数据还是在HashMap$Node中存储。</li>
<li>使用EntrySet结构为了访问方便。</li>
</ul>
<h5 id="put-添加"><a href="#put-添加" class="headerlink" title="put() 添加"></a>put() 添加</h5><blockquote>
<p>put(“键”，“值”);</p>
<p>使用put()方法可以添加键值对到指定的Map对象。</p>
</blockquote>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token class-name">Map</span> map <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">HashMap</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
map<span class="token punctuation">.</span><span class="token function">put</span><span class="token punctuation">(</span><span class="token string">"key"</span><span class="token punctuation">,</span><span class="token string">"value"</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre>





<p>get() 获得</p>
<blockquote>
<p>get(“key”)；</p>
<p>通过get()方法可以返回指定key的value值。</p>
</blockquote>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token class-name">Map</span> map <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">HashMap</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
map<span class="token punctuation">.</span><span class="token function">put</span><span class="token punctuation">(</span><span class="token string">"key"</span><span class="token punctuation">,</span><span class="token string">"value"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
map<span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token string">"key"</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span></span></code></pre>





<p>remove()</p>
<blockquote>
<p>remove(“key”);</p>
<p>根据键值对删除映射关系。</p>
</blockquote>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token class-name">Map</span> map <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">HashMap</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
map<span class="token punctuation">.</span><span class="token function">put</span><span class="token punctuation">(</span><span class="token string">"key"</span><span class="token punctuation">,</span><span class="token string">"value"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
map<span class="token punctuation">.</span><span class="token function">remove</span><span class="token punctuation">(</span><span class="token string">"key"</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span></span></code></pre>





<p>size()</p>
<blockquote>
<p>size()</p>
<p>获取元素个数</p>
</blockquote>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token class-name">Map</span> map <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">HashMap</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
map<span class="token punctuation">.</span><span class="token function">put</span><span class="token punctuation">(</span><span class="token string">"key"</span><span class="token punctuation">,</span><span class="token string">"value"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">int</span> size <span class="token operator">=</span> map<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span></span></code></pre>





<p>isEmpty()</p>
<blockquote>
<p>isEmpty();</p>
<p>判断个数是否为0</p>
</blockquote>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token class-name">Map</span> map <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">HashMap</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
map<span class="token punctuation">.</span><span class="token function">put</span><span class="token punctuation">(</span><span class="token string">"key"</span><span class="token punctuation">,</span><span class="token string">"value"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">boolean</span> i <span class="token operator">=</span> map<span class="token punctuation">.</span><span class="token function">isEmpty</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span></span></code></pre>





<p>clear()</p>
<blockquote>
<p>clear();</p>
<p>清除所有元素</p>
</blockquote>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token class-name">Map</span> map <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">HashMap</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
map<span class="token punctuation">.</span><span class="token function">put</span><span class="token punctuation">(</span><span class="token string">"key"</span><span class="token punctuation">,</span><span class="token string">"value"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
map<span class="token punctuation">.</span><span class="token function">clear</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span></span></code></pre>





<p>containsKey()</p>
<blockquote>
<p>containsKey(“key”);</p>
<p>查找键是否存在。</p>
</blockquote>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token class-name">Map</span> map <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">HashMap</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
map<span class="token punctuation">.</span><span class="token function">put</span><span class="token punctuation">(</span><span class="token string">"key"</span><span class="token punctuation">,</span><span class="token string">"value"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">boolean</span> i <span class="token operator">=</span> map<span class="token punctuation">.</span><span class="token function">containsKey</span><span class="token punctuation">(</span><span class="token string">"key"</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span></span></code></pre>



<h5 id="map遍历方式"><a href="#map遍历方式" class="headerlink" title="map遍历方式"></a>map遍历方式</h5><blockquote>
<p>Map接口存放着key和value，分别放在keySet和values中。</p>
</blockquote>
<p>一.增强for循环</p>
<blockquote>
<p>通过增强for获得keySet中的key，可以调用get方法得到对应的值。</p>
</blockquote>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token class-name">Map</span> map <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">HashMap</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
map<span class="token punctuation">.</span><span class="token function">put</span><span class="token punctuation">(</span><span class="token string">"key"</span><span class="token punctuation">,</span><span class="token string">"value"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
map<span class="token punctuation">.</span><span class="token function">put</span><span class="token punctuation">(</span><span class="token string">"key1"</span><span class="token punctuation">,</span><span class="token string">"value1"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
map<span class="token punctuation">.</span><span class="token function">put</span><span class="token punctuation">(</span><span class="token string">"key2"</span><span class="token punctuation">,</span><span class="token string">"value2"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
map<span class="token punctuation">.</span><span class="token function">put</span><span class="token punctuation">(</span><span class="token string">"key3"</span><span class="token punctuation">,</span><span class="token string">"value3"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
map<span class="token punctuation">.</span><span class="token function">put</span><span class="token punctuation">(</span><span class="token string">"key4"</span><span class="token punctuation">,</span><span class="token string">"value4"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">//取出所有key再通过Key去除对应的Value</span>
<span class="token class-name">Set</span> keyset <span class="token operator">=</span> map<span class="token punctuation">.</span><span class="token function">keySet</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token class-name">Object</span> key <span class="token operator">:</span> keyset<span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
     <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>map<span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span>key<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>二.迭代器</p>
<blockquote>
<p>存放key的是set类型的keySet集合，可以使用迭代器得到key再调用get方法获得对应key的value。</p>
</blockquote>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token class-name">Map</span> map <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">HashMap</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
      map<span class="token punctuation">.</span><span class="token function">put</span><span class="token punctuation">(</span><span class="token string">"key"</span><span class="token punctuation">,</span><span class="token string">"value"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
      map<span class="token punctuation">.</span><span class="token function">put</span><span class="token punctuation">(</span><span class="token string">"key1"</span><span class="token punctuation">,</span><span class="token string">"value1"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
      map<span class="token punctuation">.</span><span class="token function">put</span><span class="token punctuation">(</span><span class="token string">"key2"</span><span class="token punctuation">,</span><span class="token string">"value2"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
      map<span class="token punctuation">.</span><span class="token function">put</span><span class="token punctuation">(</span><span class="token string">"key3"</span><span class="token punctuation">,</span><span class="token string">"value3"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
      map<span class="token punctuation">.</span><span class="token function">put</span><span class="token punctuation">(</span><span class="token string">"key4"</span><span class="token punctuation">,</span><span class="token string">"value4"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
      <span class="token comment">//取出所有key再通过Key去除对应的Value</span>
      <span class="token class-name">Set</span> keyset <span class="token operator">=</span> map<span class="token punctuation">.</span><span class="token function">keySet</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">//获得set集合的迭代器</span>
      <span class="token class-name">Iterator</span> iterator <span class="token operator">=</span> keyset<span class="token punctuation">.</span><span class="token function">iterator</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

      <span class="token keyword">while</span> <span class="token punctuation">(</span>iterator<span class="token punctuation">.</span><span class="token function">hasNext</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
        <span class="token comment">//获得keySet的key，调用Map的get方法获得值</span>
          <span class="token class-name">Object</span> next <span class="token operator">=</span>  iterator<span class="token punctuation">.</span><span class="token function">next</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
          <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>map<span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span>next<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
      <span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>



<p>三. 获得Values</p>
<p>存储value的是collection类型的values集合。可以直接使用values方法获得value的集合。</p>
<p>获得collection类型的values集合后可以使用迭代器，增强for循环。</p>
<p>不能使用普通for循环，不提供get方法。</p>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token class-name">Map</span> map <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">HashMap</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
      map<span class="token punctuation">.</span><span class="token function">put</span><span class="token punctuation">(</span><span class="token string">"key"</span><span class="token punctuation">,</span><span class="token string">"value"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
      map<span class="token punctuation">.</span><span class="token function">put</span><span class="token punctuation">(</span><span class="token string">"key1"</span><span class="token punctuation">,</span><span class="token string">"value1"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
      map<span class="token punctuation">.</span><span class="token function">put</span><span class="token punctuation">(</span><span class="token string">"key2"</span><span class="token punctuation">,</span><span class="token string">"value2"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
      map<span class="token punctuation">.</span><span class="token function">put</span><span class="token punctuation">(</span><span class="token string">"key3"</span><span class="token punctuation">,</span><span class="token string">"value3"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
      map<span class="token punctuation">.</span><span class="token function">put</span><span class="token punctuation">(</span><span class="token string">"key4"</span><span class="token punctuation">,</span><span class="token string">"value4"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">//调用values方法获得value的集合</span>
      <span class="token class-name">Collection</span> values <span class="token operator">=</span> map<span class="token punctuation">.</span><span class="token function">values</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
      <span class="token comment">//获取其中元素可以使用迭代器，增强for循环</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>



<p>四. EntrySet</p>
<p>可以使用map中维护的Entry集合的方法获取。</p>
<ol>
<li>调用entrySet获得EntrySet对象，增强for后得到的是HashMap$Node类型的对象，但是实现了Map.Entry接口，也可以向上转型后使用getKey和getValue方法。</li>
<li>获得Entry集合后，因为Entry也是Set类型的也可以使用迭代器。使用迭代器返回的是HashMap$Node类型但是实现了Map.Entry接口，也可以向上转型后使用getKey和getValue方法。</li>
</ol>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token class-name">Map</span> map <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">HashMap</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
map<span class="token punctuation">.</span><span class="token function">put</span><span class="token punctuation">(</span><span class="token string">"key"</span><span class="token punctuation">,</span><span class="token string">"value"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
map<span class="token punctuation">.</span><span class="token function">put</span><span class="token punctuation">(</span><span class="token string">"key1"</span><span class="token punctuation">,</span><span class="token string">"value1"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
map<span class="token punctuation">.</span><span class="token function">put</span><span class="token punctuation">(</span><span class="token string">"key2"</span><span class="token punctuation">,</span><span class="token string">"value2"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
map<span class="token punctuation">.</span><span class="token function">put</span><span class="token punctuation">(</span><span class="token string">"key3"</span><span class="token punctuation">,</span><span class="token string">"value3"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
map<span class="token punctuation">.</span><span class="token function">put</span><span class="token punctuation">(</span><span class="token string">"key4"</span><span class="token punctuation">,</span><span class="token string">"value4"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token comment">//获得entryset</span>
<span class="token class-name">Set</span> entryset <span class="token operator">=</span> map<span class="token punctuation">.</span><span class="token function">entrySet</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">for</span><span class="token punctuation">(</span><span class="token class-name">Object</span> entry <span class="token operator">:</span> entryset<span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
    <span class="token comment">//entry实现了Map.Entry接口</span>
    <span class="token class-name">Map<span class="token punctuation">.</span>Entry</span> m <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token class-name">Map<span class="token punctuation">.</span>Entry</span><span class="token punctuation">)</span> entry<span class="token punctuation">;</span>
    <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>m<span class="token punctuation">.</span><span class="token function">getKey</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">+</span> <span class="token string">"-"</span> <span class="token operator">+</span> m<span class="token punctuation">.</span><span class="token function">getValue</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token class-name">Map</span> map <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">HashMap</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
map<span class="token punctuation">.</span><span class="token function">put</span><span class="token punctuation">(</span><span class="token string">"key"</span><span class="token punctuation">,</span><span class="token string">"value"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
map<span class="token punctuation">.</span><span class="token function">put</span><span class="token punctuation">(</span><span class="token string">"key1"</span><span class="token punctuation">,</span><span class="token string">"value1"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
map<span class="token punctuation">.</span><span class="token function">put</span><span class="token punctuation">(</span><span class="token string">"key2"</span><span class="token punctuation">,</span><span class="token string">"value2"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
map<span class="token punctuation">.</span><span class="token function">put</span><span class="token punctuation">(</span><span class="token string">"key3"</span><span class="token punctuation">,</span><span class="token string">"value3"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
map<span class="token punctuation">.</span><span class="token function">put</span><span class="token punctuation">(</span><span class="token string">"key4"</span><span class="token punctuation">,</span><span class="token string">"value4"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token comment">//获得entryset</span>
<span class="token class-name">Set</span> entryset <span class="token operator">=</span> map<span class="token punctuation">.</span><span class="token function">entrySet</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token class-name">Iterator</span> iterator <span class="token operator">=</span> entryset<span class="token punctuation">.</span><span class="token function">iterator</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token keyword">while</span> <span class="token punctuation">(</span>iterator<span class="token punctuation">.</span><span class="token function">hasNext</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
    <span class="token class-name">Map<span class="token punctuation">.</span>Entry</span> m <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token class-name">Map<span class="token punctuation">.</span>Entry</span><span class="token punctuation">)</span> iterator<span class="token punctuation">.</span><span class="token function">next</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>m<span class="token punctuation">.</span><span class="token function">getKey</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">+</span> <span class="token string">"-"</span> <span class="token operator">+</span> m<span class="token punctuation">.</span><span class="token function">getValue</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    
<span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>





<h4 id="41-12-HashMap"><a href="#41-12-HashMap" class="headerlink" title="41.12 HashMap"></a>41.12 HashMap</h4><p>Map接口的常用实现类：HashMap ,Hashtable和Properties。</p>
<p>HashMap是Map接口使用频率最高的实现类。</p>
<p>HashMap是以key-val对的方式存储的。</p>
<p>key不能重复，但是值可以重复，允许使用null键和null值。</p>
<p>如果添加相同的key，则会覆盖原来的key-val，等同于修改(key不会替换，val会替换)</p>
<p>与HashSet一样，不保证映射顺序，因为底层是以hash表的方式来存储的。</p>
<p>HashMap没有实现同步，因此是线程不安全的。方法上没有做同步互斥。(synchronized)</p>
<ol>
<li>(k,v)是一个Node实现了Map.Entry&lt;K,V&gt;。</li>
<li>jdk7.0的hashmap底层实现[数组+链表]，jdk8.0底层[数组+链表+红黑树]</li>
</ol>
<p>扩容机制：</p>
<ul>
<li>HashMap底层维护了Node类型的数组table，默认为null。</li>
<li>当创建对象时，将加载因子(loadfactor)初始化为0.75。</li>
<li>当添加key-val时，通过key的哈希值得到在table上的索引，然后判断索引处是否有元素，如果没有元素直接添加，如果该索引处有元素，继续判断该元素的key是否和准备加入的key相等，如果相等直接替换val；如果不相等需要判断树结构还是链表结构，做出相应的处理，如果添加时发现容量不够用，则需要扩容。</li>
<li>第一次添加，则需要扩容table容量为16，临界值为(threshold)为12.</li>
<li>以后再次扩容，则需要扩容的容量为原来的2倍。临界值为原来的两倍，即24，以此类推。</li>
<li>在java8中如果一条链表的元素个数超过TREEIFY_THRESHOLD(默认为8)，并且table的大小 &gt;= MIN_TREEIFY_CAPACITY(默认64)，就会进项树化(红黑树)。==如果小于会先扩容再树化。==</li>
</ul>
<h4 id="41-13-Hashtable"><a href="#41-13-Hashtable" class="headerlink" title="41.13 Hashtable"></a>41.13 Hashtable</h4><p>存放的元素时键值对：即k-y</p>
<p>==hashtable的键和值不能为null。== 否则还会抛出异常：NullPointerException</p>
<p>hashTable使用方法基本上和HashMap一样。</p>
<p>==HashTable是线程安全的==，hashMap是线程不安全的。<a target="_blank" rel="noopener" href="https://imgtu.com/i/b6ZYG9"><img src="https://s1.ax1x.com/2022/03/07/b6ZYG9.png" alt="b6ZYG9.png"></a></p>
<p>扩容机制：</p>
<blockquote>
<p><code>(oldCaoacity &lt;&lt;1)+1</code></p>
</blockquote>
<table>
<thead>
<tr>
<th></th>
<th>版本</th>
<th>线程安全</th>
<th>效率</th>
<th>允许null</th>
</tr>
</thead>
<tbody><tr>
<td>HashMap</td>
<td>1.2</td>
<td>不安全</td>
<td>高</td>
<td>可以</td>
</tr>
<tr>
<td>Hashtable</td>
<td>1.0</td>
<td>安全</td>
<td>较低</td>
<td>不可以</td>
</tr>
</tbody></table>
<h4 id="41-14-Properties"><a href="#41-14-Properties" class="headerlink" title="41.14 Properties"></a>41.14 Properties</h4><p>Properties类继承自Hashtable类并且实现Map接口，也是使用一种键值对的形式保存数据。</p>
<p>==继承Hashtable所以key和value不能为Null。==</p>
<p>存放是无顺序的。</p>
<p>Properties还可以用于从xxx.Properties文件中，加载数据到Prorerties类对象。</p>
<p>put() 添加修改</p>
<blockquote>
<p>put(“key”,”vale”);</p>
<p>可以通过put方法向指定Properties对象中添加键值对，如果有相同的键则会替换对应的值。</p>
</blockquote>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token class-name">Properties</span> properties <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Properties</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
properties<span class="token punctuation">.</span><span class="token function">put</span><span class="token punctuation">(</span><span class="token string">"key"</span><span class="token punctuation">,</span><span class="token string">"value"</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre>





<p>remover()</p>
<blockquote>
<p>remover(“key”);</p>
<p>可以通过remover方法向指定Properties对象中删除对应键值对。如果删除成功会返回对应的值，如果没有找到会返回null。</p>
</blockquote>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token class-name">Properties</span> properties <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Properties</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
properties<span class="token punctuation">.</span><span class="token function">put</span><span class="token punctuation">(</span><span class="token string">"key"</span><span class="token punctuation">,</span><span class="token string">"value"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token class-name">Object</span> s <span class="token operator">=</span> properties<span class="token punctuation">.</span><span class="token function">remove</span><span class="token punctuation">(</span><span class="token string">"key"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>s<span class="token punctuation">)</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span></span></code></pre>





<p>get() 查</p>
<blockquote>
<p>get(“key”);</p>
<p>可以通过get方法向指定Properties对象中查找对应键值对。如果查找成功会返回对应的值，如果没有找到会返回null。</p>
</blockquote>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token class-name">Properties</span> properties <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Properties</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
properties<span class="token punctuation">.</span><span class="token function">put</span><span class="token punctuation">(</span><span class="token string">"key"</span><span class="token punctuation">,</span><span class="token string">"value"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token class-name">Object</span> s <span class="token operator">=</span> properties<span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token string">"key"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>s<span class="token punctuation">)</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span></span></code></pre>





<h4 id="41-15-TreeMap"><a href="#41-15-TreeMap" class="headerlink" title="41.15 TreeMap"></a>41.15 TreeMap</h4><blockquote>
<p> TreeMap使用默认构造器是没有排序的存储。</p>
<p> 调用TreeMap类传入比较器的构造器会按照指定比较器来对添加的元素的==key进行排序==。(找到适当的位置差插入)</p>
</blockquote>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token class-name">TreeMap</span> treeMap <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">TreeMap</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">Comparator</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
   <span class="token annotation punctuation">@Override</span>
   <span class="token keyword">public</span> <span class="token keyword">int</span> <span class="token function">compare</span><span class="token punctuation">(</span><span class="token class-name">Object</span> o1<span class="token punctuation">,</span> <span class="token class-name">Object</span> o2<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
       <span class="token comment">//根据字符串大小排序</span>
       <span class="token keyword">return</span> <span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">)</span>o1<span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">compareTo</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">)</span>o2<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
     <span class="token comment">//根据字符串长度排序</span>
     <span class="token comment">//return ((String)o1).length - ((String)o2).length;</span>
   <span class="token punctuation">&#125;</span>
<span class="token punctuation">&#125;</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>



<pre class="line-numbers language-java" data-language="java"><code class="language-java">parent <span class="token operator">=</span> t<span class="token punctuation">;</span>
           cmp <span class="token operator">=</span> cpr<span class="token punctuation">.</span><span class="token function">compare</span><span class="token punctuation">(</span>key<span class="token punctuation">,</span> t<span class="token punctuation">.</span>key<span class="token punctuation">)</span><span class="token punctuation">;</span>
           <span class="token keyword">if</span> <span class="token punctuation">(</span>cmp <span class="token operator">&lt;</span> <span class="token number">0</span><span class="token punctuation">)</span>
               t <span class="token operator">=</span> t<span class="token punctuation">.</span>left<span class="token punctuation">;</span>
           <span class="token keyword">else</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>cmp <span class="token operator">></span> <span class="token number">0</span><span class="token punctuation">)</span>
               t <span class="token operator">=</span> t<span class="token punctuation">.</span>right<span class="token punctuation">;</span>
           <span class="token keyword">else</span>
               <span class="token keyword">return</span> t<span class="token punctuation">.</span><span class="token function">setValue</span><span class="token punctuation">(</span>value<span class="token punctuation">)</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<ul>
<li>第一次添加元素时，直接将k-y封装到Entry对象中，放入root；</li>
<li>第二次添加时候才使用比较器，将加入的key找到适当的位置存储。</li>
</ul>
<h4 id="41-16-集合选择"><a href="#41-16-集合选择" class="headerlink" title="41.16 集合选择"></a>41.16 集合选择</h4><blockquote>
<p>在开发中，选择集合实现类主要取决于业务操作特点，然后跟根据集合实现类特性进行选择：</p>
<p>先判断存储的类型(一组对象[单列]或一组键值对)</p>
</blockquote>
<ul>
<li>一组对象：Collection接口：<ul>
<li>允许重复： list接口<ul>
<li>增删多：LinkedList  (底层维护了一个双向链表)</li>
<li>改查多：ArrayList   (底层维护Object类型的可变数组)</li>
<li>线程安全： Vector</li>
</ul>
</li>
<li>不允许重复：set接口：<ul>
<li>无序： HashSet (底层是HashMap，维护了一个哈希表)</li>
<li>排序： TreeSet</li>
<li>插入和取出顺序一致：LinkedHashSet (维护数组+双向链表)</li>
</ul>
</li>
</ul>
</li>
<li>一组键值对： Map接口<ul>
<li>键无顺序：HashMap (底层是哈希表：数组 + 链表 + 红黑树)</li>
<li>键排序：TreeMap</li>
<li>键插入和键取出LinkedHashMap</li>
<li>读取文件： Properties</li>
<li>线程安全： Hashtable</li>
</ul>
</li>
</ul>

                
            </div>
            <hr/>

            

    <div class="reprint" id="reprint-statement">
        
            <div class="reprint__author">
                <span class="reprint-meta" style="font-weight: bold;">
                    <i class="fas fa-user">
                        Author:
                    </i>
                </span>
                <span class="reprint-info">
                    <a href="/about" rel="external nofollow noreferrer">凡诚</a>
                </span>
            </div>
            <div class="reprint__type">
                <span class="reprint-meta" style="font-weight: bold;">
                    <i class="fas fa-link">
                        Link:
                    </i>
                </span>
                <span class="reprint-info">
                    <a href="http://example.com/2020/05/08/java-ji-chu/">http://example.com/2020/05/08/java-ji-chu/</a>
                </span>
            </div>
            <div class="reprint__notice">
                <span class="reprint-meta" style="font-weight: bold;">
                    <i class="fas fa-copyright">
                        Reprint policy:
                    </i>
                </span>
                <span class="reprint-info">
                    All articles in this blog are used except for special statements
                    <a href="https://creativecommons.org/licenses/by/4.0/deed.zh" rel="external nofollow noreferrer" target="_blank">CC BY 4.0</a>
                    reprint polocy. If reproduced, please indicate source
                    <a href="/about" target="_blank">凡诚</a>
                    !
                </span>
            </div>
        
    </div>

    <script async defer>
      document.addEventListener("copy", function (e) {
        let toastHTML = '<span>Copied successfully, please follow the reprint policy of this article</span><button class="btn-flat toast-action" onclick="navToReprintStatement()" style="font-size: smaller">more</a>';
        M.toast({html: toastHTML})
      });

      function navToReprintStatement() {
        $("html, body").animate({scrollTop: $("#reprint-statement").offset().top - 80}, 800);
      }
    </script>



            <div class="tag_share" style="display: block;">
                <div class="post-meta__tag-list" style="display: inline-block;">
                    
                        <div class="article-tag">
                            
                                <a href="/tags/java/">
                                    <span class="chip bg-color">java</span>
                                </a>
                            
                        </div>
                    
                </div>
                <div class="post_share" style="zoom: 80%; width: fit-content; display: inline-block; float: right; margin: -0.15rem 0;">
                    <link rel="stylesheet" type="text/css" href="/libs/share/css/share.min.css">
<div id="article-share">

    
    <div class="social-share" data-sites="twitter,facebook,google,qq,qzone,wechat,weibo,douban,linkedin" data-wechat-qrcode-helper="<p>微信扫一扫即可分享！</p>"></div>
    <script src="/libs/share/js/social-share.min.js"></script>
    

    

</div>

                </div>
            </div>
            
                <style>
    #reward {
        margin: 40px 0;
        text-align: center;
    }

    #reward .reward-link {
        font-size: 1.4rem;
        line-height: 38px;
    }

    #reward .btn-floating:hover {
        box-shadow: 0 6px 12px rgba(0, 0, 0, 0.2), 0 5px 15px rgba(0, 0, 0, 0.2);
    }

    #rewardModal {
        width: 320px;
        height: 350px;
    }

    #rewardModal .reward-title {
        margin: 15px auto;
        padding-bottom: 5px;
    }

    #rewardModal .modal-content {
        padding: 10px;
    }

    #rewardModal .close {
        position: absolute;
        right: 15px;
        top: 15px;
        color: rgba(0, 0, 0, 0.5);
        font-size: 1.3rem;
        line-height: 20px;
        cursor: pointer;
    }

    #rewardModal .close:hover {
        color: #ef5350;
        transform: scale(1.3);
        -moz-transform:scale(1.3);
        -webkit-transform:scale(1.3);
        -o-transform:scale(1.3);
    }

    #rewardModal .reward-tabs {
        margin: 0 auto;
        width: 210px;
    }

    .reward-tabs .tabs {
        height: 38px;
        margin: 10px auto;
        padding-left: 0;
    }

    .reward-content ul {
        padding-left: 0 !important;
    }

    .reward-tabs .tabs .tab {
        height: 38px;
        line-height: 38px;
    }

    .reward-tabs .tab a {
        color: #fff;
        background-color: #ccc;
    }

    .reward-tabs .tab a:hover {
        background-color: #ccc;
        color: #fff;
    }

    .reward-tabs .wechat-tab .active {
        color: #fff !important;
        background-color: #22AB38 !important;
    }

    .reward-tabs .alipay-tab .active {
        color: #fff !important;
        background-color: #019FE8 !important;
    }

    .reward-tabs .reward-img {
        width: 210px;
        height: 210px;
    }
</style>

<div id="reward">
    <a href="#rewardModal" class="reward-link modal-trigger btn-floating btn-medium waves-effect waves-light red">赏</a>

    <!-- Modal Structure -->
    <div id="rewardModal" class="modal">
        <div class="modal-content">
            <a class="close modal-close"><i class="fas fa-times"></i></a>
            <h4 class="reward-title">你的赏识是我前进的动力</h4>
            <div class="reward-content">
                <div class="reward-tabs">
                    <ul class="tabs row">
                        <li class="tab col s6 alipay-tab waves-effect waves-light"><a href="#alipay">支付宝</a></li>
                        <li class="tab col s6 wechat-tab waves-effect waves-light"><a href="#wechat">微 信</a></li>
                    </ul>
                    <div id="alipay">
                        <img src="/medias/reward/alipay.jpg" class="reward-img" alt="支付宝打赏二维码">
                    </div>
                    <div id="wechat">
                        <img src="/medias/reward/wechat.png" class="reward-img" alt="微信打赏二维码">
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<script>
    $(function () {
        $('.tabs').tabs();
    });
</script>

            
        </div>
    </div>

    

    

    

    
    <div class="livere-card card" data-aos="fade-up">
    <!-- 来必力City版安装代码 -->
    <div id="lv-container" class="card-content" data-id="city" data-uid="MTAyMC81NzU4NC8zNDA0OA==">
        <script type="text/javascript">
            (function (d, s) {
                let j, e = d.getElementsByTagName(s)[0];
                if (typeof LivereTower === 'function') {
                    return;
                }

                j = d.createElement(s);
                j.src = 'https://cdn-city.livere.com/js/embed.dist.js';
                j.async = true;

                e.parentNode.insertBefore(j, e);
            })(document, 'script');
        </script>
        <noscript>为正常使用来必力评论功能请激活JavaScript。</noscript>
    </div>
    <!-- City版安装代码已完成 -->
</div>
    

    

    

    

    

<article id="prenext-posts" class="prev-next articles">
    <div class="row article-row">
        
        <div class="article col s12 m6" data-aos="fade-up">
            <div class="article-badge left-badge text-color">
                <i class="fas fa-chevron-left"></i>&nbsp;Previous</div>
            <div class="card">
                <a href="/2021/02/11/mysql-suo-yin-ji-diao-you-pian/">
                    <div class="card-image">
                        
                        
                        <img src="/medias/featureimages/2.jpg" class="responsive-img" alt="MySQL索引与调优">
                        
                        <span class="card-title">MySQL索引与调优</span>
                    </div>
                </a>
                <div class="card-content article-content">
                    <div class="summary block-with-text">
                        
                            MySQL是一个关系型数据库管理系统，由瑞典MySQL AB 公司开发，属于 Oracle 旗下产品。
                        
                    </div>
                    <div class="publish-info">
                        <span class="publish-date">
                            <i class="far fa-clock fa-fw icon-date"></i>2021-02-11
                        </span>
                        <span class="publish-author">
                            
                            <i class="fas fa-bookmark fa-fw icon-category"></i>
                            
                            <a href="/categories/%E6%95%B0%E6%8D%AE%E5%BA%93/" class="post-category">
                                    数据库
                                </a>
                            
                            
                        </span>
                    </div>
                </div>
                
                <div class="card-action article-tags">
                    
                    <a href="/tags/%E6%95%B0%E6%8D%AE%E5%BA%93/">
                        <span class="chip bg-color">数据库</span>
                    </a>
                    
                    <a href="/tags/sql/">
                        <span class="chip bg-color">sql</span>
                    </a>
                    
                    <a href="/tags/mysql/">
                        <span class="chip bg-color">mysql</span>
                    </a>
                    
                </div>
                
            </div>
        </div>
        
        
        <div class="article col s12 m6" data-aos="fade-up">
            <div class="article-badge right-badge text-color">
                Next&nbsp;<i class="fas fa-chevron-right"></i>
            </div>
            <div class="card">
                <a href="/2020/03/11/ji-suan-ji-ji-chu/">
                    <div class="card-image">
                        
                        
                        <img src="/medias/featureimages/2.jpg" class="responsive-img" alt="计算机基础">
                        
                        <span class="card-title">计算机基础</span>
                    </div>
                </a>
                <div class="card-content article-content">
                    <div class="summary block-with-text">
                        
                            计算机基础知识 从基本结构上来讲,电脑可以分为五大部分:运算器、存储器、 控制器、输入设备、输出设备。
                        
                    </div>
                    <div class="publish-info">
                            <span class="publish-date">
                                <i class="far fa-clock fa-fw icon-date"></i>2020-03-11
                            </span>
                        <span class="publish-author">
                            
                            <i class="fas fa-bookmark fa-fw icon-category"></i>
                            
                            <a href="/categories/%E8%AE%A1%E7%AE%97%E6%9C%BA%E5%9F%BA%E7%A1%80/" class="post-category">
                                    计算机基础
                                </a>
                            
                            
                        </span>
                    </div>
                </div>
                
                <div class="card-action article-tags">
                    
                    <a href="/tags/%E8%AE%A1%E7%AE%97%E6%9C%BA%E5%9F%BA%E7%A1%80/">
                        <span class="chip bg-color">计算机基础</span>
                    </a>
                    
                </div>
                
            </div>
        </div>
        
    </div>
</article>

</div>



<!-- 代码块功能依赖 -->
<script type="text/javascript" src="/libs/codeBlock/codeBlockFuction.js"></script>

<!-- 代码语言 -->

<script type="text/javascript" src="/libs/codeBlock/codeLang.js"></script>


<!-- 代码块复制 -->

<script type="text/javascript" src="/libs/codeBlock/codeCopy.js"></script>


<!-- 代码块收缩 -->

<script type="text/javascript" src="/libs/codeBlock/codeShrink.js"></script>


    </div>
    <div id="toc-aside" class="expanded col l3 hide-on-med-and-down">
        <div class="toc-widget card" style="background-color: white;">
            <div class="toc-title"><i class="far fa-list-alt"></i>&nbsp;&nbsp;TOC</div>
            <div id="toc-content"></div>
        </div>
    </div>
</div>

<!-- TOC 悬浮按钮. -->

<div id="floating-toc-btn" class="hide-on-med-and-down">
    <a class="btn-floating btn-large bg-color">
        <i class="fas fa-list-ul"></i>
    </a>
</div>


<script src="/libs/tocbot/tocbot.min.js"></script>
<script>
    $(function () {
        tocbot.init({
            tocSelector: '#toc-content',
            contentSelector: '#articleContent',
            headingsOffset: -($(window).height() * 0.4 - 45),
            collapseDepth: Number('0'),
            headingSelector: 'h2, h3, h4'
        });

        // modify the toc link href to support Chinese.
        let i = 0;
        let tocHeading = 'toc-heading-';
        $('#toc-content a').each(function () {
            $(this).attr('href', '#' + tocHeading + (++i));
        });

        // modify the heading title id to support Chinese.
        i = 0;
        $('#articleContent').children('h2, h3, h4').each(function () {
            $(this).attr('id', tocHeading + (++i));
        });

        // Set scroll toc fixed.
        let tocHeight = parseInt($(window).height() * 0.4 - 64);
        let $tocWidget = $('.toc-widget');
        $(window).scroll(function () {
            let scroll = $(window).scrollTop();
            /* add post toc fixed. */
            if (scroll > tocHeight) {
                $tocWidget.addClass('toc-fixed');
            } else {
                $tocWidget.removeClass('toc-fixed');
            }
        });

        
        /* 修复文章卡片 div 的宽度. */
        let fixPostCardWidth = function (srcId, targetId) {
            let srcDiv = $('#' + srcId);
            if (srcDiv.length === 0) {
                return;
            }

            let w = srcDiv.width();
            if (w >= 450) {
                w = w + 21;
            } else if (w >= 350 && w < 450) {
                w = w + 18;
            } else if (w >= 300 && w < 350) {
                w = w + 16;
            } else {
                w = w + 14;
            }
            $('#' + targetId).width(w);
        };

        // 切换TOC目录展开收缩的相关操作.
        const expandedClass = 'expanded';
        let $tocAside = $('#toc-aside');
        let $mainContent = $('#main-content');
        $('#floating-toc-btn .btn-floating').click(function () {
            if ($tocAside.hasClass(expandedClass)) {
                $tocAside.removeClass(expandedClass).hide();
                $mainContent.removeClass('l9');
            } else {
                $tocAside.addClass(expandedClass).show();
                $mainContent.addClass('l9');
            }
            fixPostCardWidth('artDetail', 'prenext-posts');
        });
        
    });
</script>

    

</main>




    <footer class="page-footer bg-color">
    
        <link rel="stylesheet" href="/libs/aplayer/APlayer.min.css">
<style>
    .aplayer .aplayer-lrc p {
        
        font-size: 12px;
        font-weight: 700;
        line-height: 16px !important;
    }

    .aplayer .aplayer-lrc p.aplayer-lrc-current {
        
        font-size: 15px;
        color: blue;
    }

    
    .aplayer.aplayer-fixed.aplayer-narrow .aplayer-body {
        left: -66px !important;
    }

    .aplayer.aplayer-fixed.aplayer-narrow .aplayer-body:hover {
        left: 0px !important;
    }

    
</style>
<div class="">
    
    <div class="row">
        <meting-js class="col l8 offset-l2 m10 offset-m1 s12"
                   server="netease"
                   type="playlist"
                   id="2477330090"
                   fixed='true'
                   autoplay='false'
                   theme='blue'
                   loop='all'
                   order='random'
                   preload='auto'
                   volume='0.7'
                   list-folded='true'
        >
        </meting-js>
    </div>
</div>

<script src="/libs/aplayer/APlayer.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/meting@2/dist/Meting.min.js"></script>

    
    <div class="container row center-align" style="margin-bottom: 15px !important;">
        <div class="col s12 m8 l8 copy-right">
            Copyright&nbsp;&copy;
            
                <span id="year">2022</span>
            
            <span id="year">2022</span>
            <a href="/about" target="_blank">凡诚</a>
            &nbsp;&nbsp;<a href="https://hexo.io/" target="_blank"></a>
            |&nbsp;Theme&nbsp;<a href="https://github.com/blinkfox/hexo-theme-matery" target="_blank">Matery</a>
            <br>
            
            &nbsp;<i class="fas fa-chart-area"></i>&nbsp;站点总字数:&nbsp;<span
                class="white-color">607.1k</span>&nbsp;字
            
            
            
            
            
            
            <span id="busuanzi_container_site_pv">
                |&nbsp;<i class="far fa-eye"></i>&nbsp;总访问量:&nbsp;<span id="busuanzi_value_site_pv"
                    class="white-color"></span>&nbsp;次
            </span>
            
            
            <span id="busuanzi_container_site_uv">
                |&nbsp;<i class="fas fa-users"></i>&nbsp;总访问人数:&nbsp;<span id="busuanzi_value_site_uv"
                    class="white-color"></span>&nbsp;人
            </span>
            
            <br>
            
            <span id="sitetime">载入运行时间...</span>
            <script>
                function siteTime() {
                    var seconds = 1000;
                    var minutes = seconds * 60;
                    var hours = minutes * 60;
                    var days = hours * 24;
                    var years = days * 365;
                    var today = new Date();
                    var startYear = "2022";
                    var startMonth = "11";
                    var startDate = "7";
                    var startHour = "0";
                    var startMinute = "0";
                    var startSecond = "0";
                    var todayYear = today.getFullYear();
                    var todayMonth = today.getMonth() + 1;
                    var todayDate = today.getDate();
                    var todayHour = today.getHours();
                    var todayMinute = today.getMinutes();
                    var todaySecond = today.getSeconds();
                    var t1 = Date.UTC(startYear, startMonth, startDate, startHour, startMinute, startSecond);
                    var t2 = Date.UTC(todayYear, todayMonth, todayDate, todayHour, todayMinute, todaySecond);
                    var diff = t2 - t1;
                    var diffYears = Math.floor(diff / years);
                    var diffDays = Math.floor((diff / days) - diffYears * 365);
                    var diffHours = Math.floor((diff - (diffYears * 365 + diffDays) * days) / hours);
                    var diffMinutes = Math.floor((diff - (diffYears * 365 + diffDays) * days - diffHours * hours) /
                        minutes);
                    var diffSeconds = Math.floor((diff - (diffYears * 365 + diffDays) * days - diffHours * hours -
                        diffMinutes * minutes) / seconds);
                    if (startYear == todayYear) {
                        document.getElementById("year").innerHTML = todayYear;
                        document.getElementById("sitetime").innerHTML = "本站已安全运行 " + diffDays + " 天 " + diffHours +
                            " 小时 " + diffMinutes + " 分钟 " + diffSeconds + " 秒";
                    } else {
                        document.getElementById("year").innerHTML = startYear + " - " + todayYear;
                        document.getElementById("sitetime").innerHTML = "本站已安全运行 " + diffYears + " 年 " + diffDays +
                            " 天 " + diffHours + " 小时 " + diffMinutes + " 分钟 " + diffSeconds + " 秒";
                    }
                }
                setInterval(siteTime, 1000);
            </script>
            
            <br>
            
        </div>
        <div class="col s12 m4 l4 social-link social-statis">
    <a href="https://github.com/fanshicheng" class="tooltipped" target="_blank" data-tooltip="访问我的GitHub" data-position="top" data-delay="50">
        <i class="fab fa-github"></i>
    </a>



    <a href="mailto:2639144944@qq.com" class="tooltipped" target="_blank" data-tooltip="邮件联系我" data-position="top" data-delay="50">
        <i class="fas fa-envelope-open"></i>
    </a>







    <a href="tencent://AddContact/?fromId=50&fromSubId=1&subcmd=all&uin=2639144944" class="tooltipped" target="_blank" data-tooltip="QQ联系我: 2639144944" data-position="top" data-delay="50">
        <i class="fab fa-qq"></i>
    </a>







</div>
    </div>
</footer>

<div class="progress-bar"></div>


    <!-- 搜索遮罩框 -->
<div id="searchModal" class="modal">
    <div class="modal-content">
        <div class="search-header">
            <span class="title"><i class="fas fa-search"></i>&nbsp;&nbsp;Search</span>
            <input type="search" id="searchInput" name="s" placeholder="Please enter a search keyword"
                   class="search-input">
        </div>
        <div id="searchResult"></div>
    </div>
</div>

<script type="text/javascript">
$(function () {
    var searchFunc = function (path, search_id, content_id) {
        'use strict';
        $.ajax({
            url: path,
            dataType: "xml",
            success: function (xmlResponse) {
                // get the contents from search data
                var datas = $("entry", xmlResponse).map(function () {
                    return {
                        title: $("title", this).text(),
                        content: $("content", this).text(),
                        url: $("url", this).text()
                    };
                }).get();
                var $input = document.getElementById(search_id);
                var $resultContent = document.getElementById(content_id);
                $input.addEventListener('input', function () {
                    var str = '<ul class=\"search-result-list\">';
                    var keywords = this.value.trim().toLowerCase().split(/[\s\-]+/);
                    $resultContent.innerHTML = "";
                    if (this.value.trim().length <= 0) {
                        return;
                    }
                    // perform local searching
                    datas.forEach(function (data) {
                        var isMatch = true;
                        var data_title = data.title.trim().toLowerCase();
                        var data_content = data.content.trim().replace(/<[^>]+>/g, "").toLowerCase();
                        var data_url = data.url;
                        data_url = data_url.indexOf('/') === 0 ? data.url : '/' + data_url;
                        var index_title = -1;
                        var index_content = -1;
                        var first_occur = -1;
                        // only match artiles with not empty titles and contents
                        if (data_title !== '' && data_content !== '') {
                            keywords.forEach(function (keyword, i) {
                                index_title = data_title.indexOf(keyword);
                                index_content = data_content.indexOf(keyword);
                                if (index_title < 0 && index_content < 0) {
                                    isMatch = false;
                                } else {
                                    if (index_content < 0) {
                                        index_content = 0;
                                    }
                                    if (i === 0) {
                                        first_occur = index_content;
                                    }
                                }
                            });
                        }
                        // show search results
                        if (isMatch) {
                            str += "<li><a href='" + data_url + "' class='search-result-title'>" + data_title + "</a>";
                            var content = data.content.trim().replace(/<[^>]+>/g, "");
                            if (first_occur >= 0) {
                                // cut out 100 characters
                                var start = first_occur - 20;
                                var end = first_occur + 80;
                                if (start < 0) {
                                    start = 0;
                                }
                                if (start === 0) {
                                    end = 100;
                                }
                                if (end > content.length) {
                                    end = content.length;
                                }
                                var match_content = content.substr(start, end);
                                // highlight all keywords
                                keywords.forEach(function (keyword) {
                                    var regS = new RegExp(keyword, "gi");
                                    match_content = match_content.replace(regS, "<em class=\"search-keyword\">" + keyword + "</em>");
                                });

                                str += "<p class=\"search-result\">" + match_content + "...</p>"
                            }
                            str += "</li>";
                        }
                    });
                    str += "</ul>";
                    $resultContent.innerHTML = str;
                });
            }
        });
    };

    searchFunc('/search.xml', 'searchInput', 'searchResult');
});
</script>

    <!-- 回到顶部按钮 -->
<div id="backTop" class="top-scroll">
    <a class="btn-floating btn-large waves-effect waves-light" href="#!">
        <i class="fas fa-arrow-up"></i>
    </a>
</div>

    <script type="text/javascript">
        var OriginTitile=document.title,st;
        document.addEventListener("visibilitychange",function(){
            document.hidden?(document.title="(Ő∀Ő3)ノ",clearTimeout(st)):(document.title="ヽ(●-`Д´-)ノ欢迎回来",st=setTimeout(function(){document.title=OriginTitile},3e3))
        })
    </script>
    <!-- <script src="/js/cursor.js"></script> -->
    <script type="text/javascript">
        //只在桌面版网页启用特效
        var windowWidth = $(window).width();
        if (windowWidth > 768) {
            document.write('<script type="text/javascript" src="/js/sakura.js"><\/script>');
        }
        </script>
    <script src="/libs/materialize/materialize.min.js"></script>
    <script src="/libs/masonry/masonry.pkgd.min.js"></script>
    <script src="/libs/aos/aos.js"></script>
    <script src="/libs/scrollprogress/scrollProgress.min.js"></script>
    <script src="/libs/lightGallery/js/lightgallery-all.min.js"></script>
    <script src="/js/matery.js"></script>

    <!-- Baidu Analytics -->

    <!-- Baidu Push -->

<script>
    (function () {
        var bp = document.createElement('script');
        var curProtocol = window.location.protocol.split(':')[0];
        if (curProtocol === 'https') {
            bp.src = 'https://zz.bdstatic.com/linksubmit/push.js';
        } else {
            bp.src = 'http://push.zhanzhang.baidu.com/push.js';
        }
        var s = document.getElementsByTagName("script")[0];
        s.parentNode.insertBefore(bp, s);
    })();
</script>

    
    
    <script async src="/libs/others/busuanzi.pure.mini.js"></script>
    

    

    
    <script>
        (function (i, s, o, g, r, a, m) {
            i["DaoVoiceObject"] = r;
            i[r] = i[r] || function () {
                (i[r].q = i[r].q || []).push(arguments)
            }, i[r].l = 1 * new Date();
            a = s.createElement(o), m = s.getElementsByTagName(o)[0];
            a.async = 1;
            a.src = g;
            a.charset = "utf-8";
            m.parentNode.insertBefore(a, m)
        })(window, document, "script", ('https:' == document.location.protocol ? 'https:' : 'http:') +
            "//widget.daovoice.io/widget/6984b559.js", "daovoice")
        daovoice('init', {
            app_id: "377cb7de"
        });
        daovoice('update');
    </script>
    

	
    
    <script type="text/javascript" color="0,0,255"
        pointColor="0,0,255" opacity='0.7'
        zIndex="-1" count="99"
        src="/libs/background/canvas-nest.js"></script>
    

    

    

    
    <script src="/libs/instantpage/instantpage.js" type="module"></script>
    
    <!-- 冒泡 -->
    
<canvas class="fireworks" style="position: fixed;left: 0;top: 0;z-index: 1; pointer-events: none;" ></canvas> 
<script type="text/javascript" src="//cdn.bootcss.com/animejs/2.2.0/anime.min.js"></script> 
<script type="text/javascript" src="/js/fireworks.js"></script>


</body>

</html>
